Tim
Tim

Reputation: 1

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

Crash logs are showing -

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

This is .h declaration

@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

This the .m file

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

Upvotes: 0

Views: 543

Answers (1)

zaph
zaph

Reputation: 112857

The statement:

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

Does not create a MutableDictionary, userDefaults objectForKey: returns an immutable object. Casting is insufficient, you need create a mutable dictionary with something like:

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

Upvotes: 1

Related Questions