Objective C/NSMutableDictionary - [NSCFSet objectForKey:]:无法识别的选择器

发布于 2024-10-26 11:30:22 字数 1147 浏览 1 评论 0原文

我不明白我做错了什么。我有一个字典作为单例类的属性:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

a√萤火虫的光℡ 2024-11-02 11:30:22

我会检查 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.

夜雨飘雪 2024-11-02 11:30:22

确保您已在指定的初始化程序中初始化了 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];
}

Make sure that you've initialized your soundMap in designated initializer:

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

Dont forget to override default dealloc implementation:

// class implementation file
- (void)dealloc {
  [soundMap release];
  //...release other objects you own...
  [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文