未捕获的异常"NSInternalInconsistencyException",原因:'-[__NSCFDictionary setObject:forKey:]: mutating method



崩溃日志显示 -

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");
}

语句:

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

不创建可变字典,userDefaults objectForKey:返回一个不可变的对象。强制转换是不够的,你需要创建一个可变字典,如下所示:

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

最新更新