Saving NSMutableArray to NSUserDefaults

做~自己de王妃 提交于 2020-01-06 01:35:12

问题


I have an NSMutableArray inside of my AppDelegate, and I'm attempting to save it to NSUserDefaults. The NSMutableArray (strainsfinal) contains a list of favorited items. I want this list to save, and be present even if the app is shut down, and then restarted. Does anyone know why my code isn't working (not saving my data)? See below:

AppDelegate.m

-(void)updateStrains:(NSDictionary *)item
{        
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataSave = [currentDefaults objectForKey:@"strains"];
    if (strainsfinal != nil)
    {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
        if (oldSavedArray != nil)
            strainsfinal = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        else
            strainsfinal = [[NSMutableArray alloc] init];
    }

}

EDIT: Appdelegate.m So I've changed my above method to the method below (and I thought it should work), but the app crashes, saying: "Terminating app due to uncaught exception 'NSInvalidArguementException', reason: 'NSConcreteAttributedString initwithString: nil 'value'.

What am I missing?!

-(void)updateStrains:(NSDictionary *)item {

    NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:strainsfinal];
        [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"strains"];
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        [nc postNotificationName:@"arrayupdated" object:self userInfo:nil];
        NSLog(@"updated strains %@",strainsfinal);   
}

回答1:


You must add

[currentDefaults synchronize];

each time after you save something to userDefaults;

But for such kind of data is better to use .plist files. Check documentation for more info.

EDITED Code:

-(void)updateStrains:(NSDictionary *)item {
    NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:item[@"strainsfinal"]];
    [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"strains"];
   // NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
   // [nc postNotificationName:@"arrayupdated" object:self userInfo:nil];
    NSLog(@"updated strains %@",strainsfinal);   
    [[NSUserDefaults standardUserDefaults] synchronize]; // this will save you UserDefaults
}



回答2:


use following method :

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:array1 forKey:@"DataArra1"];
[userDef setObject:array2 forKey:@"DataArr2"];
[userDef synchronize];

To retrieve the information:

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
NSArray *arrayText2 = [userDef objectForKey:@"DataArra1"];
NSArray *arrayText1 = [userDef objectForKey:@"DataArr2"];



回答3:


You should archive NSArray before saving.

[[NSUserDefaults standardUserDefaults] 
  setObject:[NSKeyedArchiver archivedDataWithRootObject:yourArray] 
     forKey:@"yourArray"];

and unarchive when getting from NSUserDefaults.

NSData *data = [currentDefaults objectForKey:@"yourArray"];
if (data != nil)
{
    NSArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}


来源:https://stackoverflow.com/questions/20236845/saving-nsmutablearray-to-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!