ios - NSMutableDictionary with nil as value -
i have project @ https://github.com/niklassaers/njsnotificationcenter far 2 unit tests. 1 of them runs, 1 of them runs 60% of time. remaining 40% of time, fail because nsmutablevalue contains nil value, though have never put in nil value (nor should possible)
the problem arises here:
- (void) addobserver:(id)observer selector:(sel)aselector name:(nsstring *)aname object:(id)anobject priority:(nsinteger)priority { njsnotificationkey *key = [[njsnotificationkey alloc] initwithobserver:observer name:aname object:anobject]; nslog(@"key is: %p", key); key.priority = priority; njsnotificationvalue *value = [[njsnotificationvalue alloc] initwithselector:aselector]; nsassert(value, @"value cannot nil!"); @synchronized(observers) { observers[key] = value; nslog(@"key: %p\tvalue: %p\t%@", key, value, observers); if(observers[key] == nil) nslog(@"this can't be!"); } }
i make key, not nil, make value, not nil, add dictionary , dictionary, nil! makes no sense me.
i have wrapped every access observers (a local instance variable) in @synchronized block in case there other threading going on (there isn't).
please check out code (bsd license) , have @ it, , me understand how can be. if you'd like, i'd love pair program on you, i'm @niklassaers on twitter
you haven't implemented hash.
https://developer.apple.com/library/ios/documentation/cocoa/conceptual/collections/articles/dictionaries.html#//apple_ref/doc/uid/20000134-sw8 keys must implement hash , isequal: methods because dictionary uses hash table organize storage , access contained objects
the dictionary copying key object , storing - when tried lookup original key object, not find because hash values not match.
Comments
Post a Comment