你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS中NSUserDefaults的用法(輕量級本地數據存儲)

IOS中NSUserDefaults的用法(輕量級本地數據存儲)

編輯:IOS開發綜合

NSUserDefaults適合存儲輕量級的本地數據,比如要保存一個登陸界面的數據,用戶名、密碼之類的,個人覺得使用NSUserDefaults是首選。下次再登陸的時候就可以直接從NSUserDefaults裡面讀取上次登陸的信息咯。

因為如果使用自己建立的plist文件什麼的,還得自己顯示創建文件,讀取文件,很麻煩,而是用NSUserDefaults則不用管這些東西,就像讀字符串一樣,直接讀取就可以了。

NSUserDefaults支持的數據格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL類型。很實用吧

NSUserDefaults很方便,讀取也很容易。下面給出一個示例看看如何使用:(PS:更詳細的也可以參考官方文檔哈)

ViewController.h文件中主要是放幾個控件,用於顯示存儲的數據:


[cpp]
 #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中調用這兩個方法就可以看出結果咯


[cpp]
#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 


 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved