您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 數據存儲三大方式:直接寫入、NSUserDefaults、歸檔

編輯:iOS 開發綜合

數據存儲(直接寫入、NSUserDefaults、NSKeyedArchiver)

iOS 中常用的文件存取方法

1、直接寫文件的方式,可存儲 NSString、NSArray、NSDictionary、NSData、NSNumber,數據全部存放在一個屬性列表文件(*.plist)中。

2、NSUserDefaults(偏好設置),用來存儲應用設置資訊,文件放在 Preferences 目錄下。

3、歸檔操作(NSKeyedArchiver),不同於前兩種,它能將自定義對象存放到文件中。

首先要明確:對於一個應用來說,有唯一與之對應的沙盒,每個應用不能跨沙盒操作文件。

一、獲取沙盒路徑並寫文件存儲數據

 #import "ViewController.h" @interface ViewController () @end @implementationViewController - (void)viewDidLoad { [super viewDidLoad]; [self mainOperation]; } - (void)mainOperation { //獲取沙盒中緩存文件夾路徑 //方法一 NSString *homePath = NSHomeDirectory(); NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches"]; //方法二 NSArray *arr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [arr lastObject]; NSString *filePath = [cachePath stringByAppendingPathComponent:@"tese.plist"]; NSLog(@"%@",filePath); //創建數據 NSDictionary *content = @{@"字典數據測試1":@"1",@"字典數據測試2":@"2",@"字典數據測試":@"3"}; [content writeToFile:filePath atomically:YES]; NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath]; NSLog(@"%@",dic); } @end

此後沙盒的 Library/Caches 目錄下會生成 tese.plist 文件:

文件內容:

二、使用 NSUserDefaults 實現數據存儲

每個應用都有一個 NSUserDefaults 實例,可用來存儲應用配置與用戶資訊,如用戶名、密碼、字體大小等,數據自動保存在沙盒的 Library/Preferences 目錄下,同樣只能存取 NSString、NSArray、NSDictionary、NSData、NSNumber 類型。

 #import "LXXViewController.h" @interface LXXViewController () @end @implementationLXXViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"NSUserDefaults Demo"; } - (IBAction)saveData:(id)sender { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:@"Lotheve" forKey:@"name"]; [userDefaults setObject:@"NSUserDefaults" forKey:@"demo"]; [userDefaults synchronize]; NSLog(@"數據已保存"); } - (IBAction)getData:(id)sender { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *name = [userDefaults objectForKey:@"name"]; NSString *demo = [userDefaults objectForKey:@"demo"]; NSLog(@"name = %@ demo =%@",name,demo); } @end

點擊「保存數據」後,數據以 plist 格式寫入磁盤:

三、NSKeyedArchiver 歸檔操作

歸檔操作最大的好處是能存儲自定義對象。需要注意的是,需要保存的對象類必須遵循 NSCoding 協議,並實現 encodeWithCoder: 與 initWithCoder: 兩個方法。

1、文件結構:

2、代碼示例:

 #import "LXXViewController.h" #import "TestPerson.h" @interface LXXViewController () @property (nonatomic ,strong) TestPerson *p; @end @implementationLXXViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)saveData:(id)sender { _p = [[TestPerson alloc]init]; _p.name = @"Lotheve"; _p.age = 20; _p.sex = @"m"; _p.familyMumbers = @[@"Father",@"Mather",@"Me"]; NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *targetPath = [docPath stringByAppendingPathComponent:@"lotheve.plist"]; [NSKeyedArchiver archiveRootObject:_p toFile:targetPath]; NSLog(@"文件已儲存"); }
 - (IBAction)getData:(id)sender { NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *targetPath = [docPath stringByAppendingPathComponent:@"lotheve.plist"]; TestPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:targetPath]; NSLog(@"name = %@ , age =%ld , sex = %@ , familyMubers = %@",person.name,person.age,person.sex,person.familyMumbers); NSLog(@"文件已提取"); } @end

TestPerson.h:

 #import  @interface TestPerson : UIViewController @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) NSInteger age; @property (nonatomic, copy) NSString *sex; @property (nonatomic, strong) NSArray *familyMumbers; @end

TestPerson.m:

 #import "TestPerson.h" @interface TestPerson () @end @implementationTestPerson - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - NSCoding協議方法 //當進行歸檔操作的時候就會調用該方法 - (void)encodeWithCoder:(NSCoder *)aCoder { NSLog(@"調用了encodeWithCoder方法"); [aCoder encodeObject:_name forKey:@"name"]; [aCoder encodeInteger:_age forKey:@"age"]; [aCoder encodeObject:_sex forKey:@"sex"]; [aCoder encodeObject:_familyMumbers forKey:@"familyMumbers"]; } //當進行解檔操作的時候就會調用該方法 - (id)initWithCoder:(NSCoder *)aDecoder { NSLog(@"調用了initWithCoder方法"); if (self = [super init]) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"]; self.sex = [aDecoder decodeObjectForKey:@"sex"]; _familyMumbers = [aDecoder decodeObjectForKey:@"familyMumbers"]; } return self; } @end

點擊「保存數據」後可查看 Documents 目錄,點擊「提取數據」後即可打印出還原的對象資訊:

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