无法将UIViewController子类添加到NSDictionary

时间:2012-02-02 19:34:15

标签: objective-c ios nsmutabledictionary

我不想将UIViewController的子类添加到NSMutableDictionary中,因为UIViewController是NSObject,所以应该没问题。

这是我的代码:

NSArray *requestingPathes = [[NSArray alloc] initWithObjects:indexPath, nil];
requestingPair = [[NSMutableDictionary alloc] initWithObjectsAndKeys:requestingPathes, dele, nil];

当代理“dele”(恰好是提到的子类)用于初始化Dictionary“requesPair”时,应用程序崩溃。

错误是:

-[MainViewController copyWithZone:]: unrecognized selector sent to instance 0x7446b30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController copyWithZone:]: unrecognized selector sent to instance 0x7446b30

0x7446b30是子类“MainViewController。

的地址

为什么会这样?

2 个答案:

答案 0 :(得分:4)

-initWithObjectsAndKeys:需要一个对象和键列表,按顺序 - 交替对象,键,对象,键,零。你正在做的是尝试将你的dele对象作为一个键,从中可以检索到值requestingPathes; NSDictionary对象在设置它们时会复制它们的键,并且视图控制器不支持被复制,因此失败了。大概你想做的事情更像是这样:

requestingPair = [[NSMutableDictionary alloc] initWithObjectsAndKeys:requestingPathes, @"paths", dele, @"delegate", nil];

然后,您可以通过调用[requestingPair objectForKey:@"delegate"][requestPair objectForKey:@"paths"]的路径来检索委托对象。

答案 1 :(得分:2)

您的词典键必须符合NSCopying协议,UIViewController(和您的MainViewController)不符合。

你想让删除是关键还是价值?这是目前的关键。

相关问题