gpt4 book ai didi

iphone - NSInvalidArgumentException/copyWithZone 异常与 NSMutableDictionary

转载 作者:行者123 更新时间:2023-12-03 18:41:13 24 4
gpt4 key购买 nike

我有一个封装 ABRecordID 的类,当它用作添加到 NSMutableDictionary 的键时,我收到运行时异常:

"NSInvalidArgumentException: *** -[MyRecordId copyWithZone:]: unrecognized selector sent to instance"

MyRecordId 声明为:

@interface MyRecordId : NSObject {
ABRecordID abRecordId;
}

-(id)initWithId:(ABRecordID)anABRecordId;
@property (nonatomic) ABRecordID abRecordId;

@end

添加到字典:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
MyRecordId *recordId = [[MyRecordId alloc] initWithId:anABRecordId];
[dict setObject:@"hello" forKey:recordId];

最后一行导致异常。我知道你不能将非对象类型存储为字典的键,但我认为将其包装在 NSObject 派生类中就可以了。

我不应该将 ABRecordID 存储在其他对象中吗?我应该做点别的吗?

最佳答案

使用 NSNumber 将 ABRecordID 存储在 Obj-C 类中:

[dict setObject:@"hello" forKey:[NSNumber numberWithInt:recordId]];

要再次获取recordId,请执行以下操作:

recordId = [[dict objectForKey:@"hello"] intValue];

关于iphone - NSInvalidArgumentException/copyWithZone 异常与 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1045270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com