NSUserDefaults教程

NSDictionary* defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];

 

if([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]==nil){

[[NSUserDefaults standardUserDefaults] setObject:@"This is message" forKey:@"message"];

}

代码意思是判断NSUserDefaults的“message”key 在dictionaryRepresentation中是否存在,如果不存在就

设置“message”

在加上句[[NSUserDefaults standardUserDefaults] synchronize];////立刻同步,没有这句,它不会立刻创建文件的,要等个时间戳之后

 

也见过别人把默认参数的设置写到applicationDidFinishLaunching注册的

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

NSDictionary *defaultValues = [NSDictionary dictionaryWithObjectsAndKeys:

@"This is message", @"message",

nil];

 

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaultValues];

}

 

 

////////////////////下边是另外一个高手的讲解/////////////////////////

例子简单点:

 

[[NSUserDefaults standardUserDefaults]setBool:YESforKey:@"sendversion"];//写入

[[NSUserDefaults standardUserDefaults]synchronize];//立刻同步,没有这句,它不会立刻创建文件的,要等个时间戳之后

 

k,你的plist文件就会有这东西了

然后你就可以判断了

 

if ([[NSUserDefaultsstandardUserDefaultsboolForKey:NSDEFAULT_SEND_VERSION] ==NO

{

你的操作

}

 

另外

在说说这个东西创建完了之后的位置(好多人找不到)

/Users/apple/Library/Application Support/iPhone Simulator/5.0/Applications/

然后里面就是你的模拟器里面各个项目的文件夹,找到你的(全是一对号码,找不到?那就都删了,留你自己那个,不就找到了吗)

然后就是下图(我自己的)xxxxxxxxx-Library-Preferences-com.xxxxx(你的项目名字)  就找到了

 

下面是大神们的讲解和例子

http://www.cocoachina.com/bbs/read.php?tid=6125&page=1  (有例子哦)

http://www.cocoachina.com/bbs/read.php?tid=9414&keyword=NSUserDefaults

posted @ 2013-08-21 17:49  jevinss  阅读(623)  评论(0编辑  收藏  举报