B.S.
B.S.

Reputation: 21726

'AnyObject' is not a subtype of 'Custom Tuple'

I receive this error message: 'AnyObject' is not a subtype of 'KeyValuePair' when I try to compile the app.

Here is some code sample:

typealias KeyValuePair = (key: String, value: String) // custom tuple

var items = [KeyValuePair]() 

func getSomeItems() -> [AnyObject]
{
    return items as [AnyObject]
}

If I change for example var items = [KeyValuePair]() to var items = [String]() obviously it works. I also tried force case as!. Doesn't work

What is wrong with this code? Is there a possibility to case some [tuple] to [AnyObject]?

Thanks in advance!

Upvotes: 0

Views: 461

Answers (2)

freezing_
freezing_

Reputation: 1044

Instead of tuple use class or simply -> (key:String,value:String)

Upvotes: 0

Dániel Nagy
Dániel Nagy

Reputation: 12015

You can cast class types to [Anyobject] (link), but the tuple type has compound type, which is not class type.

Upvotes: 1

Related Questions