简体   繁体   English

NSUserDefaults不起作用

[英]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. 首先是tableviewcontroller,其次是视图控制器。 from second view i sending data into my tableview trough perepareForSegue method. 从第二个视图,我通过perepareForSegue方法将数据发送到我的tableview中。 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. 我的数据(字符串)是NSMutableArray,我想在我的应用程序appDelegate.m中的didEnterBackgoung之后将这个MutableArray保存到UsedDefault中。然后,我想将其加载回去,但没有任何反应。 I put my code in appDidBecomeActive in appDelegate.m and i already try write it in my first view viewDidLoad, but no action. 我将代码放在appDelegate.m的appDidBecomeActive中,并且已经尝试在第一个视图viewDidLoad中编写它,但是没有任何动作。 My MuttableArray is still empty, after new app start. 新应用启动后,我的MuttableArray仍然为空。

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 flipsideView是我的第一个视图,而zoznamFunkcii是我的NSMuttable数组

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. 您正在将一个空的NSMutableArray保存到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: 并且由于NSUserDefault不会保存可变对象,因此您的恢复代码应如下所示:

- (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. 如果数组中的对象不是NSDataNSStringNSNumberNSDateNSArrayNSDictionary实例,则必须首先对数组进行编码。 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"]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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