始终加载保存的数据,并正确写入 if 函数 [英] Always loading saved data, and properly writing if function

查看:55
本文介绍了始终加载保存的数据,并正确写入 if 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加载用户数据,但我需要循环加载.(总是加载).我的代码是

I need to load user data, but I need to do it looped. (always loading). My code is

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];

而且,每当我创建一个 if 函数时,例如

And also, whenever I make an if function, for example

if (t1activity == "active")
{

//code here

}

我收到一条错误消息,提示预期标识符"或("

I get an error saying Expected identifier or '('

非常感谢!(目标-c)

Thanks so much! (objective-c)

推荐答案

如果值仅在 YES/NO 之间不同,最好提供 BOOL.

In the case the value differs only between YES/NO is better to provide a BOOL.

BOOL isUserActive = [[NSUserDefaults standardUserDefaults] boolForKey:@"preferenceName];

和set方法:

[[NSUserDefaults standardUserDefaults] 
 setBool:YES
 forKey:@"preferenceName"];

那么决定就更简单了:

if(isUserActive) {
{

//code here

}

我很想知道 NSUSerDefaults 多久更新一次.这是 Apple 文档.

I was curious to know, how often the NSUSerDefaults do update. Here the Apple Docs.

NSUserDefaults 缓存信息以避免每次需要默认值时都必须打开用户的默认数据库.同步方法会定期自动调用,使内存缓存与用户的默认数据库保持同步.

NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with a user’s defaults database.

这篇关于始终加载保存的数据,并正确写入 if 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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