我有一个具有 aNSMutableDictionary
作为属性的类:
@interface Alibi : NSObject <NSCopying>
@property (nonatomic, copy) NSMutableDictionary * alibiDetails;
@end
使用以下构造函数:
- (Alibi *)init
{
self = [super init];
_alibiDetails = [NSMutableDictionary dictionary];
return self;
}
和复制方法:
- (Alibi *)copyWithZone:(NSZone *)zone
{
Alibi *theCopy = [[Alibi alloc] init];
theCopy.alibiDetails = [self.alibiDetails mutableCopy];
return theCopy;
}
当我尝试调用setObject:ForKey:
时,出现运行时错误mutating method sent to immutable object
。
我Alibi
在视图控制器中声明了对象 as@property (copy, nonatomic) Alibi * theAlibi;
并用self.theAlibi = [[Alibi alloc] init];
in对其进行初始化viewDidLoad
。
崩溃的行是:
NSString * recipient;
recipient = @"Boss";
[self.theAlibi.alibiDetails setObject:recipient forKey:@"Recipient"];
请让我知道我在这里做错了什么。我正在为 iPhone 上的 iOS 5 编写代码。