国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

2019-10-21 18:56:45
字體:
供稿:網(wǎng)友
這篇文章主要介紹了iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個部分,需要的朋友可以參考下
 

XML屬性列表-plist
一、應(yīng)用沙盒

每個iOS應(yīng)用都有?己的應(yīng)?沙盒(應(yīng)用沙盒就是文件系統(tǒng)目錄),與其他文件系統(tǒng)隔離。應(yīng)?必須待在?己的沙盒里,其他應(yīng)用不能訪問該沙盒(提示:在IOS8中已經(jīng)開放訪問)

應(yīng)?沙盒的文件系統(tǒng)?錄,如下圖所示(假設(shè)應(yīng)用的名稱叫Layer)

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

模擬器應(yīng)?用沙盒的根路徑在: (apple是?用戶名, 7.0是模擬器版本) /Users/apple/Library/Application Support/iPhone Simulator/7.0/Applications

二、應(yīng)用沙盒結(jié)構(gòu)分析

應(yīng)?程序包:(上圖中的Layer)包含了所有的資源文件和可執(zhí)行文件

Documents:保存應(yīng)?運行時生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時會備份該目錄。例如,游戲應(yīng)用可將游戲存檔保存在該目錄

tmp:保存應(yīng)?運行時所需的臨時數(shù)據(jù),使?完畢后再將相應(yīng)的文件從該目錄刪除。應(yīng)用沒有運行時,系統(tǒng)也可能會清除該目錄下的文件。iTunes同步設(shè)備時 不會備份該目錄

Library/Caches:保存應(yīng)用運行時?成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時不會備份該目錄。?一般存儲體積大、不需要備份的非重要數(shù)據(jù)

Library/Preference:保存應(yīng)用的所有偏好設(shè)置,iOS的Settings(設(shè)置) 應(yīng)?會在該?錄中查找應(yīng)?的設(shè)置信息。iTunes同步設(shè)備時會備份該目錄

三、應(yīng)用沙盒常見的獲取方式

沙盒根目錄:NSString *home = NSHomeDirectory(); 
 Documents:(2種?方式)

利用沙盒根目錄拼接”Documents”字符串

復(fù)制代碼代碼如下:

NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; // 不建議采用,因為新版本的操作系統(tǒng)可能會修改目錄名

利用NSSearchPathForDirectoriesInDomains函數(shù)
復(fù)制代碼代碼如下:

// NSUserDomainMask 代表從用戶文件夾下找
// YES 代表展開路徑中的波浪字符“~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一個目錄跟傳入的參數(shù)匹配,所以這個集合里面只有一個元素

 

NSString *documents = [array objectAtIndex:0];

tmp:NSString *tmp = NSTemporaryDirectory();


Library/Caches:(跟Documents類似的2種?方法)

 

利用沙盒根目錄拼接”Caches”字符串

利?NSSearchPathForDirectoriesInDomains函數(shù)(將函數(shù)的第2個參數(shù)改 為:NSCachesDirectory即可)

Library/Preference:通過NSUserDefaults類存取該目錄下的設(shè)置信息

相應(yīng)的代碼:

復(fù)制代碼代碼如下:

#import "NJViewController.h"
#import "NJPerson.h"

 

@interface NJViewController ()
- (IBAction)saveDataBtnClick:(id)sender;
- (IBAction)readDataBtnClick:(id)sender;

@end


復(fù)制代碼代碼如下:

@implementation NJViewController
/**
 *   點擊保存按鈕
 */
- (IBAction)saveDataBtnClick:(id)sender {
    
    // youtube做法
//    NSString *path = @"/Users/apple/Library/Application Support/iPhone Simulator/7.1/Applications/A6D53E11-DDF0-4392-B2D4-FE77A96888A6/Documents/abc.plist";
    
    // 獲取應(yīng)用程序根目錄
    NSString *home = NSHomeDirectory();
    
    // 不建議寫/
    //NSString *path = [home stringByAppendingString:@"/Documents"];
    // 不建議Documents寫死
    //NSString *path = [home stringByAppendingPathComponent:@"Documents"];
    
    // NSUserDomainMask 在用戶目錄下查找
    // YES 代表用戶目錄的~
    // NSDocumentDirectory 查找Documents文件夾
    // 建議使用如下方法動態(tài)獲取
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接文件路徑
    NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"];
    NSLog(@"%@", path);
    
    
    //NSArray *arr = @[@"lnj", @"28"];
    //[arr writeToFile:path atomically:YES];
    
    // NSDictionary *dict = @{@"name": @"lnj", @"age":@"28"};
    // 調(diào)用writeToFile將數(shù)據(jù)寫入文件
    // [dict writeToFile:path atomically:YES];
    
    /*
     plist只能存儲系統(tǒng)自帶的一些常規(guī)的類, 也就是有writeToFile方法的對象才可以使用plist保存數(shù)據(jù)
     字符串/字典/數(shù)據(jù)/NSNumber/NSData ...
     */
    
    // 自定義的對象不能保存到plist中
    NJPerson *p = [[NJPerson alloc] init];
    p.name =@"lnj";
    
    NSDictionary *dict = @{@"person": @"abc"};
    [dict writeToFile:path atomically:YES];
}
/**
 *   點擊讀取按鈕
 */
- (IBAction)readDataBtnClick:(id)sender {
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"]
    ;
    // 讀取數(shù)據(jù)
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@", dict);
}
@end

四、屬性列表

 

屬性列表是一種XML格式的文件,拓展名為plist

如果對象是NSString、NSDictionary、NSArray、NSData、 NSNumber等類型,就可以使用writeToFile:atomically:?法 直接將對象寫到屬性列表文件中


NSKeydeArchiver歸檔
一、簡單說明

在使用plist進行數(shù)據(jù)存儲和讀取,只適用于系統(tǒng)自帶的一些常用類型才能用,且必須先獲取路徑相對麻煩;
偏好設(shè)置(將所有的東西都保存在同一個文件夾下面,且主要用于存儲應(yīng)用的設(shè)置信息)
歸檔:因為前兩者都有一個致命的缺陷,只能存儲常用的類型。歸檔可以實現(xiàn)把自定義的對象存放在文件中。
二、代碼示例

1.文件結(jié)構(gòu)

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

 

復(fù)制代碼代碼如下:

//
//  YYViewController.m
//  02-歸檔
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYViewController.h"
#import "YYPerson.h"

@interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender;

@end


復(fù)制代碼代碼如下:

@implementation YYViewController

 

- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (IBAction)saveBtnOnclick:(id)sender {
    //1.創(chuàng)建對象
    YYPerson *p=[[YYPerson alloc]init];
    p.name=@"文頂頂";
    p.age=23;
    p.height=1.7;
    
    //2.獲取文件路徑
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
    NSLog(@"path=%@",path);
    
    //3.將自定義的對象保存到文件中
    [NSKeyedArchiver archiveRootObject:p toFile:path];
    
}

- (IBAction)readBtnOnclick:(id)sender {
    //1.獲取文件路徑
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
    NSLog(@"path=%@",path);
    
    //2.從文件中讀取對象
    YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
}
@end


新建一個person類

 

YYPerson.h文件

復(fù)制代碼代碼如下:

//
//  YYPerson.h
//  02-歸檔
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import <Foundation/Foundation.h>

// 如果想將一個自定義對象保存到文件中必須實現(xiàn)NSCoding協(xié)議
@interface YYPerson : NSObject<NSCoding>

//姓名
@property(nonatomic,copy)NSString *name;
//年齡
@property(nonatomic,assign)int age;
//身高
@property(nonatomic,assign)double height;
@end


YYPerson.m文件
復(fù)制代碼代碼如下:

//
//  YYPerson.m
//  02-歸檔
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYPerson.h"

@implementation YYPerson

// 當將一個自定義對象保存到文件的時候就會調(diào)用該方法
// 在該方法中說明如何存儲自定義對象的屬性
// 也就說在該方法中說清楚存儲自定義對象的哪些屬性
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    NSLog(@"調(diào)用了encodeWithCoder:方法");
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    [aCoder encodeDouble:self.height forKey:@"height"];
}

// 當從文件中讀取一個對象的時候就會調(diào)用該方法
// 在該方法中說明如何讀取保存在文件中的對象
// 也就是說在該方法中說清楚怎么讀取文件中的對象
-(id)initWithCoder:(NSCoder *)aDecoder
{
    NSLog(@"調(diào)用了initWithCoder:方法");
    //注意:在構(gòu)造方法中需要先初始化父類的方法
    if (self=[super init]) {
        self.name=[aDecoder decodeObjectForKey:@"name"];
        self.age=[aDecoder decodeIntegerForKey:@"age"];
        self.height=[aDecoder decodeDoubleForKey:@"height"];
    }
    return self;
}
@end


3.打印效果和兩個重要的錯誤提示

 

點擊保存按鈕和讀取按鈕,成功打印結(jié)果如下:

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

關(guān)于不實現(xiàn)兩個協(xié)議方法的錯誤提示:

-(void)encodeWithCoder:(NSCoder *)aCoder方法:

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

-(id)initWithCoder:(NSCoder *)aDecoder方法:

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

三、繼承類中的使用

新建一個學生類,讓這個類繼承自Preson這個類,增加一個體重的屬性。

YYstudent.h文件

復(fù)制代碼代碼如下:

//
//  YYstudent.h
//  02-歸檔
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYPerson.h"

@interface YYstudent : YYPerson
//增加一個體重屬性
@property(nonatomic,assign) double weight;
@end


YYstudent.m文件
復(fù)制代碼代碼如下:

//
//  YYstudent.m
//  02-歸檔
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYstudent.h"

@implementation YYstudent

//在子類中重寫這兩個方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    NSLog(@"調(diào)用了YYStudent encodeWithCoder");
    [aCoder encodeFloat:self.weight forKey:@"weight"];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"調(diào)用了YYstudent initWithCoder");
        self.weight = [aDecoder decodeFloatForKey:@"weight"];
    }
    return self;
}
@end


YYViewController.m文件
復(fù)制代碼代碼如下:

//
//  YYViewController.m
//  02-歸檔
//
//  Created by apple on 14-6-7.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

 

#import "YYViewController.h"
#import "YYPerson.h"
#import "YYstudent.h"

@interface YYViewController ()
- (IBAction)saveBtnOnclick:(id)sender;
- (IBAction)readBtnOnclick:(id)sender;

@end


復(fù)制代碼代碼如下:

@implementation YYViewController

 

- (void)viewDidLoad
{
    [super viewDidLoad];
}


- (IBAction)saveBtnOnclick:(id)sender {
    //1.創(chuàng)建對象
//    YYPerson *p=[[YYPerson alloc]init];
//    p.name=@"文頂頂";
//    p.age=23;
//    p.height=1.7;
    
    YYstudent *s=[[YYstudent alloc]init];
    s.name=@"wendingding";
    s.age=23;
    s.height=1.7;
    s.weight=62;
    //2.獲取文件路徑
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
    NSLog(@"path=%@",path);
    
    //3.將自定義的對象保存到文件中
//    [NSKeyedArchiver archiveRootObject:p toFile:path];
     [NSKeyedArchiver archiveRootObject:s toFile:path];
    
}

- (IBAction)readBtnOnclick:(id)sender {
    //1.獲取文件路徑
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
    NSLog(@"path=%@",path);
    
    //2.從文件中讀取對象
//    YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
//    NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
    YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@,%d,%.1f,%f",s.name,s.age,s.height,s.weight);
}
@end


點擊保存按鈕和讀取按鈕后的打印輸出:

 

深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式

 

四、重要說明

1.保存數(shù)據(jù)過程:

復(fù)制代碼代碼如下:

//1.創(chuàng)建對象
    YYstudent *s=[[YYstudent alloc]init];
    s.name=@"wendingding";
    s.age=23;
    s.height=1.7;
    s.weight=62;
    
    //2.獲取文件路徑
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
    NSLog(@"path=%@",path);
    
    //3.將自定義的對象保存到文件中
     [NSKeyedArchiver archiveRootObject:s toFile:path];

2.讀取數(shù)據(jù)過程:
復(fù)制代碼代碼如下:

//1.獲取文件路徑
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
    //2.從文件中讀取對象
    YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

3.遵守NSCoding協(xié)議,并實現(xiàn)該協(xié)議中的兩個方法。

 

4.如果是繼承,則子類一定要重寫那兩個方法。因為person的子類在存取的時候,會去子類中去找調(diào)用的方法,沒找到那么它就去父類中找,所以最后保存和讀取的時候新增加的屬性會被忽略。需要先調(diào)用父類的方法,先初始化父類的,再初始化子類的。

5.保存數(shù)據(jù)的文件的后綴名可以隨意命名。

6.通過plist保存的數(shù)據(jù)是直接顯示的,不安全。通過歸檔方法保存的數(shù)據(jù)在文件中打開是亂碼的,更安全。



注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 益阳市| 丹东市| 镇巴县| 右玉县| 辉县市| 鲁甸县| 福贡县| 招远市| 赤城县| 临湘市| 巴彦淖尔市| 正镶白旗| 鄯善县| 城市| 长武县| 石屏县| 德阳市| 衡东县| 光山县| 金山区| 彭山县| 平山县| 沅江市| 湘乡市| 布尔津县| 和平县| 瑞丽市| 黄平县| 武强县| 常州市| 苍南县| 深州市| 和林格尔县| 宁陵县| 曲麻莱县| 手游| 石城县| 大同市| 调兵山市| 新乡市| 井研县|