Objective c NSMutableDictionary与“一起崩溃”;发送到不可变对象的变异消息“;
我有一个类,它的属性是Objective c NSMutableDictionary与“一起崩溃”;发送到不可变对象的变异消息“;,objective-c,ios,ios5,nsmutabledictionary,Objective C,Ios,Ios5,Nsmutabledictionary,我有一个类,它的属性是NSMutableDictionary: @interface Alibi : NSObject <NSCopying> @property (nonatomic, copy) NSMutableDictionary * alibiDetails; @end 和复制方法: - (Alibi *)copyWithZone:(NSZone *)zone { Alibi *theCopy = [[Alibi alloc] init]; theCopy
NSMutableDictionary
:
@interface Alibi : NSObject <NSCopying>
@property (nonatomic, copy) NSMutableDictionary * alibiDetails;
@end
和复制方法:
- (Alibi *)copyWithZone:(NSZone *)zone
{
Alibi *theCopy = [[Alibi alloc] init];
theCopy.alibiDetails = [self.alibiDetails mutableCopy];
return theCopy;
}
当我试图调用setObject:ForKey:
时,我得到一个运行时错误mutating方法,该方法被发送到不可变对象
我在视图控制器中将Alibi
对象声明为@property(copy,nonatomic)Alibi*theAlibi我用self.theAlibi=[[Alibi alloc]init]初始化它代码>在viewDidLoad
中
崩溃的线路是:
NSString * recipient;
recipient = @"Boss";
[self.theAlibi.alibiDetails setObject:recipient forKey:@"Recipient"];
请让我知道我做错了什么。我正在为iPhone上的iOS 5编写代码。您有一个“copy”属性,这意味着-您的NSMutableDictionary将调用-copy方法,并在分配给合成实例变量之前返回一个常规NSDictionary。提供有关解决此问题的一些选项的信息。为了完成此线程,我将在下面包括我修订的不在场证明类,这是我所要求的。如果有人注意到任何内存泄漏或其他问题,我们将不胜感激
@implementation Alibi
NSMutableDictionary *_details;
- (Alibi *)init
{
self = [super init];
_details = [NSMutableDictionary dictionary];
return self;
}
- (NSMutableDictionary *)copyDetails
{
return [_details mutableCopy];
}
- (NSMutableDictionary *)setDetails:(NSMutableDictionary *)value
{
_details = value;
return value;
}
- (void)addDetail:(id)value forKey:(id)key
{
[_details setObject:value forKey:key];
}
- (id)getDetailForKey:(id)key
{
return [_details objectForKey:key];
}
- (Alibi *)copyWithZone:(NSZone *)zone
{
Alibi *theCopy = [[Alibi alloc] init];
theCopy.serverId = [self.serverId copyWithZone:zone];
theCopy.user = [self.user copyWithZone:zone];
theCopy.startTime = [self.startTime copyWithZone:zone];
theCopy.endTime = [self.endTime copyWithZone:zone];
[theCopy setDetails:[self copyDetails]];
return theCopy;
}
@end
@implementation Alibi
NSMutableDictionary *_details;
- (Alibi *)init
{
self = [super init];
_details = [NSMutableDictionary dictionary];
return self;
}
- (NSMutableDictionary *)copyDetails
{
return [_details mutableCopy];
}
- (NSMutableDictionary *)setDetails:(NSMutableDictionary *)value
{
_details = value;
return value;
}
- (void)addDetail:(id)value forKey:(id)key
{
[_details setObject:value forKey:key];
}
- (id)getDetailForKey:(id)key
{
return [_details objectForKey:key];
}
- (Alibi *)copyWithZone:(NSZone *)zone
{
Alibi *theCopy = [[Alibi alloc] init];
theCopy.serverId = [self.serverId copyWithZone:zone];
theCopy.user = [self.user copyWithZone:zone];
theCopy.startTime = [self.startTime copyWithZone:zone];
theCopy.endTime = [self.endTime copyWithZone:zone];
[theCopy setDetails:[self copyDetails]];
return theCopy;
}
@end