2011-03-27 39 views
1

我不明白我做錯了什麼。我有一本字典作爲一個單獨的類的屬性:Objective C/NSMutableDictionary - [NSCFSet objectForKey:]:無法識別的選擇器

@interface CABResourceManager : NSObject 
{ 
.... 
    NSMutableDictionary* soundMap; 
} 
@property (retain) NSMutableDictionary *soundMap; 

然後我對象添加到這個字典中的一類方法:

+ (void)loadSoundFromInfo:(ABSoundInfo)sound 
{ 
    static unsigned int currentSoundID = 0; 
    CABSound* newSound = [[CABSound alloc] initWithInfo:(ABSoundInfo)sound soundID:++currentSoundID]; 
    [[CABResourceManager sharedResMgr].soundMap setObject:newSound forKey:sound.name]; 
} 

,並得到它的另一種方法:

+ (ALuint)playSoundByName:(NSString*)name 
{ 
    NSMutableDictionary* map = [CABResourceManager sharedResMgr].soundMap; 
    CABSound *sound = [map objectForKey:name]; // here comes the exception 

並且應用程序退出異常。

2011-03-27 20:46:53.943 Book3HD-EN[5485:207] *** -[NSCFSet objectForKey:]: unrecognized selector sent to instance 0x226950 
2011-03-27 20:46:53.945 Book3HD-EN[5485:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException' 

我想這可能與內存管理的東西,但票數它看起來清楚我:CABSound對象做的setObject(),它不應該在這個時候公佈保留在字典。

回答

1

我會檢查soundMap是否已正確初始化。它看起來像soundMap是一個錯誤的指針,當你得到的錯誤。它可能碰巧在+ loadSoundFromInfo中爲零,這不會立即產生錯誤。

0

確保你已經初始化在指定初始化您soundMap:

// - (id) init... or something else 
soundMap = [[NSMutableDictionary alloc] init]; 

不要忘記覆蓋默認的dealloc實現:

// class implementation file 
- (void)dealloc { 
    [soundMap release]; 
    //...release other objects you own... 
    [super dealloc]; 
} 
相關問題