简体   繁体   中英

NSUserDefaults doesn't work

I am new in programming and i have one problem. I create app, with two views. First is tableviewcontroller and second is view controller. from second view i sending data into my tableview trough perepareForSegue method. My data (Strings) are NSMutableArray and this MutableArray i want to save into UsedDefault after my app didEnterBackgoung in appDelegate.m Then i want to load it back, but nothing happened. I put my code in appDidBecomeActive in appDelegate.m and i already try write it in my first view viewDidLoad, but no action. My MuttableArray is still empty, after new app start.

here is my code:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.flipsideView.zoznamFunkcii forKey:@"NSMutableArray"];
[defaults synchronize];
}

flipsideView is my first View and zoznamFunkcii is my NSMuttable array

and this is my load method:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
self.flipsideView.zoznamFunkcii = [NSMutableArray arrayWithArray:[defaults  objectForKey:@"NSMutableArray"]];
}

Can anyone help me with that? I don't know why it not work.

You are saving an empty NSMutableArray to NSUserDefaults. So of course the loaded array will be empty as well.

Here is your saving code with comments:

// overwrite zoznamFunkcii with empty array
self.flipsideView.zoznamFunkcii = [[NSMutableArray alloc]init];

NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];

// save this empty array to nsuserdefaults
[defaults setObject:self.flipsideView.zoznamFunkcii forKey:@"NSMutableArray"];

your saving code should probably look like this:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:self.flipsideView.zoznamFunkcii forKey:@"NSMutableArray"];
}

and because NSUserDefault does not save mutable objects your restoration code should look like this:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSUserDefaults*defaults = [NSUserDefaults standardUserDefaults];
    self.flipsideView.zoznamFunkcii = [NSMutableArray arrayWithArray:[defaults objectForKey:@"NSMutableArray"]];
}

and if the objects inside your array are not instances of NSData , NSString , NSNumber , NSDate , NSArray , or NSDictionary you have to encode your array first. See this question for details on how to do that.

Here you can Save Object With NSUserDefault..

[[NSUserDefaults standardUserDefaults]setObject:[NSKeyedArchiver archivedDataWithRootObject:self.flipsideView.zoznamFunkcii] forKey:@"NSMutableArray"];
    [[NSUserDefaults standardUserDefaults]synchronize];

and you make coder and decoder method

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.toolResult = [decoder decodeObjectForKey:@"obj1"];
        self.toolNotes = [decoder decodeObjectForKey:@"obj2"];

     }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.toolResult forKey:@"obj1"];
    [encoder encodeObject:self.toolNotes forKey:@"obj2"];

}

your Data Save Into NsuserDefault When you Get Data From NsUserDefault then you Use like
[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"NSMutableArray"]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM