转自 http://blog.csdn.net/enuola/article/details/7959767
NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码之类的,个人觉得使用NSUserDefaults是首选。下次再登陆的时候就可以直接从NSUserDefaults里面读取上次登陆的信息咯。
因为如果使用自己建立的plist文件什么的,还得自己显示创建文件,读取文件,很麻烦,而是用NSUserDefaults则不用管这些东西,就像读字符串一样,直接读取就可以了。
NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型。很实用吧
NSUserDefaults很方便,读取也很容易。下面给出一个示例看看如何使用:(PS:更详细的也可以参考官方文档哈)
ViewController.h文件中主要是放几个控件,用于显示存储的数据:
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 { 5 6 IBOutlet UILabel *txtInteger; 7 IBOutlet UILabel *txtFloat; 8 IBOutlet UILabel *txtDouble; 9 IBOutlet UILabel *txtNSString; 10 IBOutlet UILabel *txtNSDate; 11 IBOutlet UILabel *txtNSArray; 12 IBOutlet UILabel *txtNSDictionary; 13 } 14 15 @end
ViewController.m文件中最重要的是两个方法:
saveNSUserDefaults:用于将各种类型数据保存到NSUserDefaults中
readNSUserDefautls:用于从NSUserDefaults中读取各种类型的数据。在viewDidLoad中调用这两个方法就可以看出结果咯
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad 10 { 11 [super viewDidLoad]; 12 13 [self saveNSUserDefaults]; //调用此方法将各种数据存储到NSUserDefautls中,在下面定义 14 [self readNSUserDefaults]; //调用此方法从NSUserDefautls中读取各种数据,在下面定义 15 } 16 17 - (void)viewDidUnload 18 { 19 [txtNSString release]; 20 txtNSString = nil; 21 [txtNSDate release]; 22 txtNSDate = nil; 23 [txtNSArray release]; 24 txtNSArray = nil; 25 [txtNSDictionary release]; 26 txtNSDictionary = nil; 27 [txtInteger release]; 28 txtInteger = nil; 29 [txtFloat release]; 30 txtFloat = nil; 31 [txtDouble release]; 32 txtDouble = nil; 33 [super viewDidUnload]; 34 // Release any retained subviews of the main view. 35 } 36 37 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 38 { 39 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 40 } 41 42 - (void)dealloc { 43 [txtNSString release]; 44 [txtNSDate release]; 45 [txtNSArray release]; 46 [txtNSDictionary release]; 47 [txtInteger release]; 48 [txtFloat release]; 49 [txtDouble release]; 50 [super dealloc]; 51 } 52 53 //保存数据到NSUserDefaults 54 -(void)saveNSUserDefaults 55 { 56 NSString *myString = @"enuola"; 57 int myInteger = 100; 58 float myFloat = 50.0f; 59 double myDouble = 20.0; 60 NSDate *myDate = [NSDate date]; 61 NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world", nil]; 62 NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"enuo", @"20", nil] forKeys:[NSArray arrayWithObjects:@"name", @"age", nil]]; 63 64 //将上述数据全部存储到NSUserDefaults中 65 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 66 //存储时,除NSNumber类型使用对应的类型意外,其他的都是使用setObject:forKey: 67 [userDefaults setInteger:myInteger forKey:@"myInteger"]; 68 [userDefaults setFloat:myFloat forKey:@"myFloat"]; 69 [userDefaults setDouble:myDouble forKey:@"myDouble"]; 70 71 [userDefaults setObject:myString forKey:@"myString"]; 72 [userDefaults setObject:myDate forKey:@"myDate"]; 73 [userDefaults setObject:myArray forKey:@"myArray"]; 74 [userDefaults setObject:myDictionary forKey:@"myDictionary"]; 75 76 //这里建议同步存储到磁盘中,但是不是必须的 77 [userDefaults synchronize]; 78 79 } 80 81 //从NSUserDefaults中读取数据 82 -(void)readNSUserDefaults 83 { 84 NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults]; 85 86 //读取数据到各个label中 87 //读取整型int类型的数据 88 NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"]; 89 txtInteger.text = [NSString stringWithFormat:@"%d",myInteger]; 90 91 //读取浮点型float类型的数据 92 float myFloat = [userDefaultes floatForKey:@"myFloat"]; 93 txtFloat.text = [NSString stringWithFormat:@"%f",myFloat]; 94 95 //读取double类型的数据 96 double myDouble = [userDefaultes doubleForKey:@"myDouble"]; 97 txtDouble.text = [NSString stringWithFormat:@"%f",myDouble]; 98 99 //读取NSString类型的数据 100 NSString *myString = [userDefaultes stringForKey:@"myString"]; 101 txtNSString.text = myString; 102 103 //读取NSDate日期类型的数据 104 NSDate *myDate = [userDefaultes valueForKey:@"myDate"]; 105 NSDateFormatter *df = [[NSDateFormatter alloc] init]; 106 [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 107 txtNSDate.text = [NSString stringWithFormat:@"%@",[df stringFromDate:myDate]]; 108 109 //读取数组NSArray类型的数据 110 NSArray *myArray = [userDefaultes arrayForKey:@"myArray"]; 111 NSString *myArrayString = [[NSString alloc] init]; 112 for(NSString *str in myArray) 113 { 114 NSLog(@"str= %@",str); 115 myArrayString = [NSString stringWithFormat:@"%@ %@", myArrayString, str]; 116 [myArrayString stringByAppendingString:str]; 117 // [myArrayString stringByAppendingFormat:@"%@",str]; 118 NSLog(@"myArrayString=%@",myArrayString); 119 } 120 txtNSArray.text = myArrayString; 121 122 //读取字典类型NSDictionary类型的数据 123 NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"]; 124 NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]]; 125 txtNSDictionary.text = myDicString; 126 } 127 128 @end
好了,运行一下,可以看到xib文件中的各种数据已经绑定上了吧?
再次运行的时候,可以把viewDidLoad中的 [self saveNSUserDefaults]; 这一行注释掉,让程序直接读取而不存储数据,发现以前保存的数据仍然可以读取到界面上。
嘻嘻,很简单吧,就这样就可以是实现数据的存储了。
下面讲一下原理:
你可能会问一个问题:NSUserDefautls将数据存储在什么地方了???我都没有显示的指定路径???很疑惑吧。。。。
用NSUserDefaults存储的数据下次程序运行的时候依然存在,它把数据存储在什么地方了?如何能够清除?