NSUserDefaults在设置之前返回 [英] NSUserDefaults returns before it has been set

查看:47
本文介绍了NSUserDefaults在设置之前返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个包含两个字符串和一个int的应用程序.我希望将它们显示在另一个视图上.

Im making an app that takes two strings and an int. I want these to be displayed on another view.

我将它们放入一个数组中,我试图将它们存储在NSUserDefaults中.我设法保存了这些数据.我遇到的问题是,当我更改数据并再次保存它时,获取数组的函数在设置它的函数之前被调用.因此,应用程序始终显示以前的数据.

I have put them into an array and Im trying to store them in the NSUserDefaults. I have managed to get this data stored. The problem that im having is that when I change the data and save it again The function that gets the array is called before the one that sets it . So the application always displays the previous data.

我认为通过更改函数的调用时间可以很容易地解决问题.

I thought It would be easy to fix by changing when the functions are called.

但是从代码看来,我认为它们的顺序正确.

but from the code it looks to me lie they are in the right order.

2012-09-26 13:55:02.764 BeerDivider[4377:907] array returnd = (
    2,
    "Person 1",
    "Person 2"
)
2012-09-26 13:55:02.773 BeerDivider[4377:907] array saved = (
    5,
    "Person 1",
    "Person 2"
)

我从记录数组可以看到以错误的顺序调用了它们.

I can see from logging the array that they are called in the wrong order.

任何人都可以看到即时通讯出现问题的地方.这是我的第一篇Objective-c/xcode文章,所以不确定您要查看什么代码.我将全部放入.

Please can anyone see where im going wrong. This is my first objective-c/xcode post so not sure what code you want to see. I will put in all of it.

http://pastebin.com/BX5gqp17

很抱歉,如果这是很多代码.

Sorry if this is a lot of code.

感谢您的帮助.

更新代码

我认为发生的情况与此 iOS执行按钮的操作类似prepareSegue之前的动作

I think what is happening is similar to this iOS do the button's action before prepareSegue

推荐答案

首先查看

First Take a look on Apple documentation of NSUserDefault
NSUserDefault always return the data which you saved in it

不要忘记写 [默认同步];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:yourArray forKey:@"YourKey"]
    [defaults synchronize];

根据您提供的代码.

-(void)saveInfo:(NSArray *)myArray
    {
         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:myArray forKey:@"array"];
    [defaults synchronize];
    NSLog (@"array saved = %@", [defaults objectForKey:@"array"]);
    }  

现在检查结果

这篇关于NSUserDefaults在设置之前返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆