Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSMutableDictionary与“一起崩溃”;发送到不可变对象的变异消息“;_Objective C_Ios_Ios5_Nsmutabledictionary - Fatal编程技术网

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
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