轉自 http://blog.csdn.net/enuola/article/details/7959767html
NSUserDefaults適合存儲輕量級的本地數據,好比要保存一個登錄界面的數據,用戶名、密碼之類的,我的以爲使用NSUserDefaults是首選。下次再登錄的時候就能夠直接從NSUserDefaults裏面讀取上次登錄的信息咯。數組
由於若是使用本身創建的plist文件什麼的,還得本身顯示建立文件,讀取文件,很麻煩,而是用NSUserDefaults則不用管這些東西,就像讀字符串同樣,直接讀取就能夠了。ide
NSUserDefaults支持的數據格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL類型。很實用吧spa
NSUserDefaults很方便,讀取也很容易。下面給出一個示例看看如何使用:(PS:更詳細的也能夠參考官方文檔哈).net
ViewController.h文件中主要是放幾個控件,用於顯示存儲的數據:code
#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文件中最重要的是兩個方法:orm
saveNSUserDefaults:用於將各類類型數據保存到NSUserDefaults中
htm
readNSUserDefautls:用於從NSUserDefaults中讀取各類類型的數據。在viewDidLoad中調用這兩個方法就能夠看出結果咯blog
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self saveNSUserDefaults]; //調用此方法將各類數據存儲到NSUserDefautls中,在下面定義 [self readNSUserDefaults]; //調用此方法從NSUserDefautls中讀取各類數據,在下面定義 } - (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]; // Release any retained subviews of the main view. } - (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]; } //保存數據到NSUserDefaults -(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中 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; //存儲時,除NSNumber類型使用對應的類型意外,其餘的都是使用setObject:forKey: [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]; } //從NSUserDefaults中讀取數據 -(void)readNSUserDefaults { NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults]; //讀取數據到各個label中 //讀取整型int類型的數據 NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"]; txtInteger.text = [NSString stringWithFormat:@"%d",myInteger]; //讀取浮點型float類型的數據 float myFloat = [userDefaultes floatForKey:@"myFloat"]; txtFloat.text = [NSString stringWithFormat:@"%f",myFloat]; //讀取double類型的數據 double myDouble = [userDefaultes doubleForKey:@"myDouble"]; txtDouble.text = [NSString stringWithFormat:@"%f",myDouble]; //讀取NSString類型的數據 NSString *myString = [userDefaultes stringForKey:@"myString"]; txtNSString.text = myString; //讀取NSDate日期類型的數據 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類型的數據 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]; // [myArrayString stringByAppendingFormat:@"%@",str]; NSLog(@"myArrayString=%@",myArrayString); } txtNSArray.text = myArrayString; //讀取字典類型NSDictionary類型的數據 NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"]; NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]]; txtNSDictionary.text = myDicString; } @end
好了,運行一下,能夠看到xib文件中的各類數據已經綁定上了吧?ci
再次運行的時候,能夠把viewDidLoad中的 [self saveNSUserDefaults]; 這一行註釋掉,讓程序直接讀取而不存儲數據,發現之前保存的數據仍然能夠讀取到界面上。
嘻嘻,很簡單吧,就這樣就能夠是實現數據的存儲了。
下面講一下原理:
你可能會問一個問題:NSUserDefautls將數據存儲在什麼地方了???我都沒有顯示的指定路徑???很疑惑吧。。。。
用NSUserDefaults存儲的數據下次程序運行的時候依然存在,它把數據存儲在什麼地方了?如何可以清除?
其實它存儲在應用程序內置的一個plist文件裏,這個能夠根據路徑看到。
好比說這個是你的程序沙盒位置
/UsersLibrary/Application Support/iPhoneSimulator/4.1/Applicati*****/29788E40-AF47-45A0-8E92-3AC0F501B7F4/,(這個是應用程序對應在mac上的位置)
這個下面有/Library/Prefereces,裏面有個plist文件,存儲的就是你的userDefaults
想要刪掉的話,用removeObjectForKey或者刪掉沙盒,也就是你的應用程序而後從新安裝。
花開花謝春無論,水暖水寒魚自知