开发者

nsmutabledictionnary with nsmutablestring forkey

I have a nsmutabledictionnary, and when I use setObject:forKey: with a nsstring key it works 开发者_C百科when I try objectForKey, but if I put a nsmutablestring in the key argument of the SetObject:forKey: method, it's not work when I call objectForKey, it return the last object in the dictionnary...


You should know that NSDictionary (and NSMutableDictionary) copies the key before adding it - thus, if you use a NSMutableString as a key, then modify that same string, the key in the database will still have the old value. This is done for a reason - the key in a NSDictionary should always be static, since it's actually the hash of it that's used for retrieving the associated value.


If a normal string works, but the mutable one doesn't, then why don't you just add another line of code and convert it to a normal string?

NSString *keyValue = mutableStringVariable;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜