NSMutableDIctionary setObject:当Key是Object的成员时,ForKey崩溃 [英] NSMutableDIctionary setObject: ForKey crashing when Key is member of Object

查看:123
本文介绍了NSMutableDIctionary setObject:当Key是Object的成员时,ForKey崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSMutableDictionary将我的自定义类对象与UIButton配对,因为它是关键。作为键的UIButton也是我想要存储为NSMutableDictionary中对象的对象的成员。

I'm trying to use an NSMutableDictionary to pair my custom classes object with a UIButton as it's key. The UIButton that is the key is also a member of the object that I want to store as the object in the NSMutableDictionary.

我的类定义看起来像:

@interface ClassA : NSObject {
@public
    UIButton *button1;
    UIButton *button2;
    UILabel *label;
}

@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;
@property (nonatomic, retain) UILabel *label;
@end

我的实施就是这样:

@implementation ClassA
@synthesize button1, button2, label;
@end

NSMutableDictionary在另一个类中。我在实现中定义它如下:

The NSMutableDictionary is in another class. I define it in the implementation like this:

@interface UIClass1 : UIViewController {
    NSMutableDictionary *Dictionary;
}

-(void)DoWork;

@property (nonatomic, retain) NSMutableDictionary *Dictionary;
@end

我正在尝试设置字典值的部分已完成这里:

And the part where I'm trying to set the Dictionary values is done here:

-(void)DoWork{
    Dictionary = [[NSMutableDictionary alloc] init];
    ObjectA = [[ClassA alloc] init];

    ObjectA->button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ObjectA->button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ObjectA->label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 20, 20, 20)] autorelease]

    /* THIS IS A TEST AND IT WORKS FINE */
    NSString *str = [[NSString alloc] initWithFormat:@"test"];
    [Dictionary setObject:obj1 forKey:str];

    /*THIS IS WHAT I WOULD ACTUALLY LIKE TO DO */
    [Dictionary setObject:Object1 forKey:ObjectA->button1];
    [Dictionary setObject:ObjectA forKey:ObjectA->button2];
}

我通过我的调试器跟踪了这段代码,当我到达这一行时:

I've followed this code through my debugger and when I get to this line:

[Dictionary setObject:Object1 forKey:ObjectA->button1];

它只是崩溃了SIGABRT。我的变量都不是零,每个对象都已分配。

It just crashes SIGABRT. None of my variables are nil, every object has been allocated.

为什么我不能从ClassA设置按钮键的任何想法,但我可以将它设置为我作为测试创建的NSString?

Any ideas as to why I can't set the key to the button from ClassA but I can set it the the NSString I created as a test?

推荐答案

用作NSMutableDictionary中的键的对象必须符合NSCopying协议。

The object you use as a key in an NSMutableDictionary must conform to the NSCopying protocol.

来自Apple文档:


价值的关键。密钥被复制(使用copyWithZone:;密钥必须符合NSCopying协议的
)。密钥不能为零。

The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). The key must not be nil.

UIButton不符合NSCopying。

UIButton does not conform to NSCopying.

这篇关于NSMutableDIctionary setObject:当Key是Object的成员时,ForKey崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆