我有一個項目https://github.com/niklassaers/NJSNotificationCenter到目前爲止只有兩個單元測試。其中一個運行,其中一個運行60%的時間。時間剩下的40%,這將失敗,因爲我NSMutableValue包含一個零值,儘管我從來沒有把在零值(也不應該說是可能的)NSMutableDictionary與零作爲值
的問題就出現在這裏:
- (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 be nil!");
@synchronized(observers) {
observers[key] = value;
NSLog(@"Key: %p\tValue: %p\t%@", key, value, observers);
if(observers[key] == nil)
NSLog(@"This can't be!");
}
}
我做了一個關鍵,它不是零,我做了一個值,它不是零,我將它添加到我的字典中並從字典中取回,但現在它是零!這對我來說沒有意義。
我已經在@synchronized塊中包裝了每個對observers(一個本地實例變量)的訪問權限,以防萬一有其他線程正在進行(沒有)。
請查看我的代碼(BSD許可證)並查看它,並幫助我理解這可能是什麼。如果你願意,我很樂意將這個程序與你配對,我是Twitter上的@niklassaers
你真的實例化一個字典實例嗎? –
我懷疑這段文字:NJSNotificationValue * value = [[NJSNotificationValue alloc] initWithSelector:aSelector]; – yunas
@ColinMorelli:我是,觀察員不是零。如果你看看init方法,它會在那裏實例化。 – niklassaers