在NSMutableDictionary中将委托类设置为Key

时间:2011-06-18 10:34:29

标签: iphone cocoa-touch

我正在尝试创建一个NSMutableDictionary,其中包含由UIViewController委托组成的Keys,如下所示:

-(void) registerAsLocationManagerDelegate:(id<RTALocationManagerDelegate>)lmDelegate forPeriodicUpdates:(NSTimeInterval)seconds 
{
    NSTimer* periodicTimer = [NSTimer scheduledTimerWithTimeInterval:seconds
                                                              target:self
                                                            selector:@selector(runPeriodicUpdates:) 
                                                            userInfo:nil 
                                                             repeats:YES];
    [periodicUpdateDelegates setObject:periodicTimer forKey:lmDelegate];
}

但是,我的代码因崩溃而崩溃:

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [RTALocationsListViewController copyWithZone:]:无法识别的选择器发送到实例0x1a9750'

有什么办法吗?尝试这个我做错了吗?我应该采用不同的方法吗?谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

虽然NSDictionary复制密钥,但CFDictionary函数却没有。改为使用CFDictionary(或利用“免费桥接”)。

答案 1 :(得分:-1)

NSDictionary密钥可以是任何对象,只要它们符合NSCopying协议。

您得到的错误是因为它copyWithZone是此协议的一部分,并且您的委托对象可能无法实现它。 - 所以它不能成为关键。

您的选择是获取类的字符串表示形式并将其用作键 - 但如果您有多个同一类的委托(因为那时键不是唯一的),这可能不起作用。或者,在委托类中实现NSCopying协议。