NSUserDefaults不保存数据目标C.

时间:2016-04-01 06:16:17

标签: ios objective-c nsuserdefaults

我正在创建此应用程序,其中包含用户得分。当他们退出应用程序时,将保存分数。我通过NSUserDefaults这样做,工作正常。问题是用户得分可以在其他视图中操作。例如,他们可以使用他们的分数来购买东西等。为此,我将在NSUserDefaults中保存分数,然后在下一个视图中检索保存的数据。问题是由于某种原因没有保存数据。我有一个计时器,每秒运行一次,保存得分,当我点击按钮进入下一个视图时,我使用IBAction保存它。这是我用来初始化变量的内容:

score = [[[NSUserDefaults standardUserDefaults] objectForKey:@"score"] longLongValue];

这是为了保存它:

[[NSUserDefaults standardUserDefaults] setObject:@(score) forKey:@"score"];

我正在使用一个long long变量,这就是为什么我把它作为一个对象而不是整数。我不明白为什么在更改视图时值会发生变化。无论何时我操纵得分值,我都会保存它。在每个视图的顶部,我宣布它。我意识到其他人发布了类似的问题,我已经查看了所有问题。那些回答没有解决我的问题,这就是我在这里问的原因。同步

[[NSUserDefaults standardUserDefaults] synchronize];

对我来说也没有用。

2 个答案:

答案 0 :(得分:2)

只有你遗失的是,synchronize你没有告诉NSUserDefault在光盘上写数据。

// *** Get your score value ***
score = [[[NSUserDefaults standardUserDefaults] objectForKey:@"score"] longLongValue];

// *** manipulate it if needed ***
score+=100;

// *** save it back to `NSUserDefaults` ***
[[NSUserDefaults standardUserDefaults] setObject:@(score) forKey:@"score"];

// *** Synchronize your updates to `NSUserDefaults` by calling it, it immediately write data to disc ***
[[NSUserDefaults standardUserDefaults] synchronize];

有关NSUserDefaults阅读Apple Documentation

的详细信息

答案 1 :(得分:1)

使用它。这将保存值

$ arc diff --trace
 ARGV  '/Users/yangyan/Meican/arcanist/bin/../scripts/arcanist.php' 'diff' '--trace'
 LOAD  Loaded "phutil" from "/Users/yangyan/Meican/libphutil/src".
 LOAD  Loaded "arcanist" from "/Users/yangyan/Meican/arcanist/src".
Config: Reading user configuration file "/Users/yangyan/.arcrc"...
Config: Did not find system configuration at "/etc/arcconfig".
Working Copy: Reading .arcconfig from "/Users/yangyan/Meican/go/src/code.meican.com/diffusion/DEMETER/demeter.git/.arcconfig".
Working Copy: Path "/Users/yangyan/Meican/go/src/code.meican.com/diffusion/DEMETER/demeter.git" is part of `git` working copy "/Users/yangyan/Meican/go/src/code.meican.com/diffusion/DEMETER/demeter.git".
Working Copy: Project root is at "/Users/yangyan/Meican/go/src/code.meican.com/diffusion/DEMETER/demeter.git".
Config: Did not find local configuration at "/Users/yangyan/Meican/go/src/code.meican.com/diffusion/DEMETER/demeter.git/.git/arc/config".
>>> [0] <conduit> user.whoami() <bytes = 117>
>>> [1] <http> https://code.meican.com/api/user.whoami
<<< [1] <http> 636,235 us
<<< [0] <conduit> 636,726 us

[2016-04-01 06:33:53] EXCEPTION: (ConduitClientException) ERR-INVALID-SESSION: Session key is not present. at [<phutil>/src/conduit/ConduitFuture.php:58]
arcanist(head=master, ref.master=fcc11b3a2781), phutil(head=master, ref.master=3024f0a4908b)
  #0 ConduitFuture::didReceiveResult(array) called at [<phutil>/src/future/FutureProxy.php:58]
  #1 FutureProxy::getResult() called at [<phutil>/src/future/FutureProxy.php:35]
  #2 FutureProxy::resolve() called at [<phutil>/src/conduit/ConduitClient.php:58]
  #3 ConduitClient::callMethodSynchronous(string, array) called at [<arcanist>/src/workflow/ArcanistWorkflow.php:332]
  #4 ArcanistWorkflow::authenticateConduit() called at [<arcanist>/scripts/arcanist.php:354]