无法将 UIViewController 子类添加到 NSDictionary

发布于 2025-01-02 02:45:50 字数 740 浏览 1 评论 0原文

我不想将 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 技术交流群。

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

发布评论

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

评论(2

农村范ル 2025-01-09 02:45:50

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

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

然后您可以通过调用 [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 your dele object as a key from which the value requestingPathes 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:

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

You can then retrieve the delegate object by calling [requestingPair objectForKey:@"delegate"] and the paths with [requestPair objectForKey:@"paths"].

满栀 2025-01-09 02:45:50

您的字典键必须符合 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文