2011-03-27 354 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]; 
} 
相关问题