user
user

Reputation: 251

How to set an object as key in NSMutableDictionary in swift

I need to set an object as key in NSMutableDictionary. I declared my dictionary as

var streamsDict : NSMutableDictionary = [:]

and adding object as

 streamsDict[stream] = "Value"

but i am getting following error due to which my application crashes

Could not cast value of type 'OTStream' (0x100638590) to 'NSCopying' (0x19e0fb9e8)

I also tried

streamsDict[stream as! NSCopying] = "Value"

but again i am getting errors

`Argument type 'OTStream' does not conform to expected type 'NSCopying'`

Please suggest

Upvotes: 1

Views: 1319

Answers (2)

hariszaman
hariszaman

Reputation: 8424

Probably you do not store your object directly in dictionary but first serialize all its properties to NSDictionary

Upvotes: 0

yaakov
yaakov

Reputation: 5851

Only an object that conforms to NSCopying can be used as the key in an NSMutableDictionary.

Your OTStream object must implement the NSCopying protocol if you want to use it as the key in a dictionary.

You can find the NSCopying documentation here.

Upvotes: 1

Related Questions