无法将 UIViewController 子类添加到 NSDictionary
我不想将 UIViewController 的子类添加到 NSMutableDictionary 中,这应该没有问题,因为 UIViewController 是一个 NSObject。
这是我的代码:
NSArray *requestingPathes = [[NSArray alloc] initWithObjects:indexPath, nil];
requestingPair = [[NSMutableDictionary alloc] initWithObjectsAndKeys:requestingPathes, dele, nil];
当使用委托“dele”(恰好是提到的子类)初始化字典“requestingPair”时,应用程序崩溃。
错误是:
-[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”的地址。
为什么会出现这种情况?
I wan't to add a subclass of UIViewController to a NSMutableDictionary which should be no problem since UIViewController is a NSObject.
This is my code:
NSArray *requestingPathes = [[NSArray alloc] initWithObjects:indexPath, nil];
requestingPair = [[NSMutableDictionary alloc] initWithObjectsAndKeys:requestingPathes, dele, nil];
The app crashes when the delegate "dele" (which happens to be the mentioned subclass) is used to initialize the Dictionary "requestingPair".
The error is:
-[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 is the address of the subclass "MainViewController.
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-initWithObjectsAndKeys:
需要一个对象和键的列表,按该顺序交替出现:对象、键、对象、键、nil。您正在做的就是尝试将您的dele
对象作为键,从中可以检索值requestingPathes
; NSDictionary 对象在设置它们时会复制它们的键,并且您的视图控制器不支持复制,因此这是失败的。想必您想要做的事情更像是这样:然后您可以通过调用
[requestingPair objectForKey:@"delegate"]
和带有[requestPair objectForKey:@" 的路径来检索委托对象路径”]
。-initWithObjectsAndKeys:
expects a list of objects and keys, in that order—alternating object, key, object, key, nil. What you’re doing is trying to give it yourdele
object as a key from which the valuerequestingPathes
can be retrieved; NSDictionary objects copy their keys when you set them, and your view controller doesn’t support being copied, so that’s failing. Presumably what you want to do is something more like this:You can then retrieve the delegate object by calling
[requestingPair objectForKey:@"delegate"]
and the paths with[requestPair objectForKey:@"paths"]
.您的字典键必须符合 NSCopying 协议,而 UIViewController (和您的 MainViewController)则不符合该协议。
您希望 dele 成为键还是值?目前是关键。
Your dictionary key must conform to the NSCopying protocol, which UIViewController (and your MainViewController) doesn't.
Do you want dele to be the key or the value? It's currently the key.