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() 保留在字典中,此时不应该释放它。
I do not understand what I am doing wrong. I have a dictionary as a property of a singleton class:
@interface CABResourceManager : NSObject
{
....
NSMutableDictionary* soundMap;
}
@property (retain) NSMutableDictionary *soundMap;
Then I add an object to this dictionary in one class method:
+ (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];
}
And try to get it in another method:
+ (ALuint)playSoundByName:(NSString*)name
{
NSMutableDictionary* map = [CABResourceManager sharedResMgr].soundMap;
CABSound *sound = [map objectForKey:name]; // here comes the exception
and the app exits on exception by that.
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'
I guess it might have something with memory management, but hier it looks clear for me: CABSound object is retained in dictionary by doing setObject(), it should not be released at this time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会检查 soundMap 是否已正确初始化。当您收到错误时,看起来 soundMap 是一个错误的指针。 +loadSoundFromInfo 中可能恰好为 nil,这不会立即产生错误。
I'd check that soundMap is properly initialized. It looks like soundMap is a bad pointer at the time you get the error. It might happen to be nil in +loadSoundFromInfo, which wouldn't produce an error right away.
确保您已在指定的初始化程序中初始化了 soundMap:
不要忘记覆盖默认的 dealloc 实现:
Make sure that you've initialized your soundMap in designated initializer:
Dont forget to override default dealloc implementation: