NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码之类的,个人觉得使用NSUserDefaults是首选。下次再登陆的时候就可以直接从NSUserDefaults里面读取上次登陆的信息咯。
因为如果使用自己建立的plist文件什么的,还得自己显示创建文件,读取文件,很麻烦,而是用NSUserDefaults则不用管这些东西,就像读字符串一样,直接读取就可以了。
NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型。很实用吧
NSUserDefaults很方便,读取也很容易。下面给出一个示例看看如何使用:(PS:更详细的也可以参考官方文档哈)
ViewController.h文件中主要是放几个控件,用于显示存储的数据:
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- {
-
- IBOutlet UILabel *txtInteger;
- IBOutlet UILabel *txtFloat;
- IBOutlet UILabel *txtDouble;
- IBOutlet UILabel *txtNSString;
- IBOutlet UILabel *txtNSDate;
- IBOutlet UILabel *txtNSArray;
- IBOutlet UILabel *txtNSDictionary;
- }
-
- @end
ViewController.m文件中最重要的是两个方法:
saveNSUserDefaults:用于将各种类型数据保存到NSUserDefaults中
readNSUserDefautls:用于从NSUserDefaults中读取各种类型的数据。在viewDidLoad中调用这两个方法就可以看出结果咯
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- [self saveNSUserDefaults];
- [self readNSUserDefaults];
- }
-
- - (void)viewDidUnload
- {
- [txtNSString release];
- txtNSString = nil;
- [txtNSDate release];
- txtNSDate = nil;
- [txtNSArray release];
- txtNSArray = nil;
- [txtNSDictionary release];
- txtNSDictionary = nil;
- [txtInteger release];
- txtInteger = nil;
- [txtFloat release];
- txtFloat = nil;
- [txtDouble release];
- txtDouble = nil;
- [super viewDidUnload];
-
- }
-
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- }
-
- - (void)dealloc {
- [txtNSString release];
- [txtNSDate release];
- [txtNSArray release];
- [txtNSDictionary release];
- [txtInteger release];
- [txtFloat release];
- [txtDouble release];
- [super dealloc];
- }
-
-
- -(void)saveNSUserDefaults
- {
- NSString *myString = @"enuola";
- int myInteger = 100;
- float myFloat = 50.0f;
- double myDouble = 20.0;
- NSDate *myDate = [NSDate date];
- NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world", nil];
- NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"enuo", @"20", nil] forKeys:[NSArray arrayWithObjects:@"name", @"age", nil]];
-
-
- NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
-
- [userDefaults setInteger:myInteger forKey:@"myInteger"];
- [userDefaults setFloat:myFloat forKey:@"myFloat"];
- [userDefaults setDouble:myDouble forKey:@"myDouble"];
-
- [userDefaults setObject:myString forKey:@"myString"];
- [userDefaults setObject:myDate forKey:@"myDate"];
- [userDefaults setObject:myArray forKey:@"myArray"];
- [userDefaults setObject:myDictionary forKey:@"myDictionary"];
-
-
- [userDefaults synchronize];
-
- }
-
-
- -(void)readNSUserDefaults
- {
- NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
-
-
-
- NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"];
- txtInteger.text = [NSString stringWithFormat:@"%d",myInteger];
-
-
- float myFloat = [userDefaultes floatForKey:@"myFloat"];
- txtFloat.text = [NSString stringWithFormat:@"%f",myFloat];
-
-
- double myDouble = [userDefaultes doubleForKey:@"myDouble"];
- txtDouble.text = [NSString stringWithFormat:@"%f",myDouble];
-
-
- NSString *myString = [userDefaultes stringForKey:@"myString"];
- txtNSString.text = myString;
-
-
- NSDate *myDate = [userDefaultes valueForKey:@"myDate"];
- NSDateFormatter *df = [[NSDateFormatter alloc] init];
- [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
- txtNSDate.text = [NSString stringWithFormat:@"%@",[df stringFromDate:myDate]];
-
-
- NSArray *myArray = [userDefaultes arrayForKey:@"myArray"];
- NSString *myArrayString = [[NSString alloc] init];
- for(NSString *str in myArray)
- {
- NSLog(@"str= %@",str);
- myArrayString = [NSString stringWithFormat:@"%@ %@", myArrayString, str];
- [myArrayString stringByAppendingString:str];
-
- NSLog(@"myArrayString=%@",myArrayString);
- }
- txtNSArray.text = myArrayString;
-
-
- NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"];
- NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]];
- txtNSDictionary.text = myDicString;
- }
-
- @end
好了,运行一下,可以看到xib文件中的各种数据已经绑定上了吧?
再次运行的时候,可以把viewDidLoad中的 [self saveNSUserDefaults]; 这一行注释掉,让程序直接读取而不存储数据,发现以前保存的数据仍然可以读取到界面上。
嘻嘻,很简单吧,就这样就可以是实现数据的存储了。
下面讲一下原理:
你可能会问一个问题:NSUserDefautls将数据存储在什么地方了???我都没有显示的指定路径???很疑惑吧。。。。
用NSUserDefaults存储的数据下次程序运行的时候依然存在,它把数据存储在什么地方了?如何能够清除?
其实它存储在应用程序内置的一个plist文件里,这个可以根据路径看到。
比如说这个是你的程序沙盒位置
/UsersLibrary/Application Support/iPhoneSimulator/4.1/Applicati*****/29788E40-AF47-45A0-8E92-3AC0F501B7F4/,(这个是应用程序对应在mac上的位置)
这个下面有/Library/Prefereces,里面有个plist文件,存储的就是你的userDefaults
想要删掉的话,用removeObjectForKey或者删掉沙盒,也就是你的应用程序然后重新安装。