未捕获的异常'NSInternalInconsistencyException',原因:' - [__ NSCFDictionary setObject:forKey:]:mutating方法发送到不可变对象'

时间:2015-03-07 13:41:55

标签: ios objective-c

崩溃日志正在显示 -

uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

这是.h声明

@interface AutoUploader : NSObject
{
    BOOL isReachable;
    BOOL isViaWiFi;
    BOOL isVia3G;
    BOOL isDownloading;
}

@property (nonatomic, readonly, getter = isReachable) BOOL reachable;
@property (nonatomic, readonly, getter = isViaWiFi) BOOL viaWiFi;
@property (nonatomic, readonly, getter = isVia3G) BOOL via3G;

@property (assign) BOOL *willWipeOut;

+ (AutoUploader *) sharedInstance;

- (void) upload;
- (void) startListening;

- (void) downloadNotes;
- (void) downloadChecklists;

- (void) uploadChecklist;

@end

这是.m文件

-(void)wipeOut:(NSArray*)result{
if(LOG_ENABLED) NSLog(@"success download archives RESULT: %@", result);

BOOL __block willDeleteData = NO;
NSUserDefaults __block *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary __block *wipeOutListDict = (NSMutableDictionary*)     [userDefaults objectForKey:@"wipeOutList"];

if(wipeOutListDict==nil){
    wipeOutListDict = [[NSMutableDictionary alloc] init];
}

[result enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL  *stop) {
    NSString *dateArchived = [dict objectForKey:@"date_archived"];
    if(LOG_ENABLED) NSLog(@"date_archived : %@", dateArchived);

    if([wipeOutListDict objectForKey:dateArchived] == nil){
        willDeleteData = YES;
        [wipeOutListDict setObject:dateArchived forKey:dateArchived]; *<= ERROR Appears to be HERE*****
        [userDefaults setObject:wipeOutListDict forKey:@"wipeOutList"];
    }
}];

if(willDeleteData){
    if(LOG_ENABLED) NSLog(@"WILL WIPEOUT DATA");
    [self deleteAllData];
}else{
    if(LOG_ENABLED) NSLog(@"WILL NOT WIPEOUT DATA");
}

1 个答案:

答案 0 :(得分:1)

声明:

NSMutableDictionary __block *wipeOutListDict = (NSMutableDictionary*) [userDefaults objectForKey:@"wipeOutList"];

不创建MutableDictionary,userDefaults objectForKey:返回不可变对象。转换是不够的,你需要创建一个类似于:

的可变字典
NSMutableDictionary __block *wipeOutListDict = [(NSMutableDictionary*) [userDefaults objectForKey:@"wipeOutList"] mutableCopy];