欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2015年12月11日 09:27:18   作者:文頂頂  
這篇文章主要介紹了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)

2015121192447046.png (348×169)

模擬器應(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)

2015121192529907.png (1392×334)

復(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é)果如下:

2015121192550090.png (882×137)

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

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

2015121192609232.png (429×303)

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

2015121192625411.png (427×283)

三、繼承類中的使用

新建一個學(xué)生類,讓這個類繼承自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


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

2015121192647864.png (879×179)

四、重要說明

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 UITableViewCell詳解及按鈕點擊事件處理實例

    IOS UITableViewCell詳解及按鈕點擊事件處理實例

    這篇文章主要介紹了IOS UITableViewCell詳解及按鈕點擊事件處理實例的相關(guān)資料,這里附有示例代碼,大家可以看下如何實現(xiàn)按鍵點擊事件,需要的朋友可以參考下
    2016-12-12
  • IOS圖片的原生(Graphics)詳解及實例

    IOS圖片的原生(Graphics)詳解及實例

    這篇文章主要介紹了IOS圖片的原生(Graphics)詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • iOS讀取txt文件出現(xiàn)中文亂碼的解決方法

    iOS讀取txt文件出現(xiàn)中文亂碼的解決方法

    這篇文章主要為大家詳細介紹了iOS讀取txt文件出現(xiàn)中文亂碼的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 如何在iphon IOS設(shè)備上使用二維碼

    如何在iphon IOS設(shè)備上使用二維碼

    深度解析iPhone ios設(shè)備上使用二維碼是本文要介紹的內(nèi)容,二維碼是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號信息的。不多說,我們直接來腳本之家學(xué)習(xí)內(nèi)容詳解,感興趣的朋友一起來關(guān)注吧
    2015-08-08
  • IOS實現(xiàn)微信授權(quán)登錄功能

    IOS實現(xiàn)微信授權(quán)登錄功能

    微信是一個在開發(fā)中經(jīng)常會使用到的平臺,比如微信登錄、授權(quán)、支付、分享。今天我們來看看如何在自己的應(yīng)用里面集成微信授權(quán),需要的朋友參考下吧
    2017-03-03
  • iOS版微信朋友圈識別圖片位置信息 如何實現(xiàn)?

    iOS版微信朋友圈識別圖片位置信息 如何實現(xiàn)?

    這篇文章主要為大家詳細介紹了iOS版微信朋友圈識別圖片位置信息的實現(xiàn)方法
    2016-10-10
  • 詳解iOS的數(shù)據(jù)存儲

    詳解iOS的數(shù)據(jù)存儲

    本文介紹iOS中常用的應(yīng)用數(shù)據(jù)存儲方式及其詳細用法,內(nèi)容很全面和詳細,對大家在IOS開發(fā)中很有幫助,下面一起來看看。
    2016-08-08
  • Swift 訪問權(quán)限的資料整理

    Swift 訪問權(quán)限的資料整理

    這篇文章主要介紹了Swift 訪問權(quán)限的資料整理的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • iOS模塊化開發(fā)淺析

    iOS模塊化開發(fā)淺析

    本文給大家分析了IOS在模塊發(fā)開發(fā)時候的相關(guān)注意點以及簡單代碼做了分享,有興趣的朋友參考學(xué)習(xí)下。
    2018-04-04
  • iOS中UIRefreshControl的基本使用詳解

    iOS中UIRefreshControl的基本使用詳解

    最近在應(yīng)用中用到UIRefreshControl,覺著有必要給大家總結(jié)介紹一下這個控件,所以下面這篇文章主要給大家介紹了關(guān)于iOS中UIRefreshControl的基本使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-04-04

最新評論