XML屬性列表-plist
一囤热、應(yīng)用沙盒 每個(gè)iOS應(yīng)用都有?己的應(yīng)?沙盒(應(yīng)用沙盒就是文件系統(tǒng)目錄),與其他文件系統(tǒng)隔離。應(yīng)?必須待在?己的沙盒里,其他應(yīng)用不能訪問(wèn)該沙盒(提示:在IOS8中已經(jīng)開(kāi)放訪問(wèn))
應(yīng)?沙盒的文件系統(tǒng)?錄,如下圖所示(假設(shè)應(yīng)用的名稱叫Layer)
模擬器應(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)?運(yùn)行時(shí)生成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時(shí)會(huì)備份該目錄十气。例如,游戲應(yīng)用可將游戲存檔保存在該目錄
tmp:保存應(yīng)?運(yùn)行時(shí)所需的臨時(shí)數(shù)據(jù),使?完畢后再將相應(yīng)的文件從該目錄刪除丧蘸。應(yīng)用沒(méi)有運(yùn)行時(shí),系統(tǒng)也可能會(huì)清除該目錄下的文件窟勃。iTunes同步設(shè)備時(shí) 不會(huì)備份該目錄
Library/Caches:保存應(yīng)用運(yùn)行時(shí)?成的需要持久化的數(shù)據(jù),iTunes同步設(shè)備時(shí)不會(huì)備份該目錄祖乳。?一般存儲(chǔ)體積大、不需要備份的非重要數(shù)據(jù)
Library/Preference:保存應(yīng)用的所有偏好設(shè)置,iOS的Settings(設(shè)置) 應(yīng)?會(huì)在該?錄中查找應(yīng)?的設(shè)置信息秉氧。iTunes同步設(shè)備時(shí)會(huì)備份該目錄
三眷昆、應(yīng)用沙盒常見(jiàn)的獲取方式
沙盒根目錄:NSString *home = NSHomeDirectory();
Documents:(2種?方式)
利用沙盒根目錄拼接”Documents”字符串
<u>復(fù)制代碼</u>代碼如下:
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; // 不建議采用,因?yàn)樾掳姹镜牟僮飨到y(tǒng)可能會(huì)修改目錄名
利用NSSearchPathForDirectoriesInDomains函數(shù)
<u>復(fù)制代碼</u>代碼如下:
// NSUserDomainMask 代表從用戶文件夾下找
// YES 代表展開(kāi)路徑中的波浪字符“~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一個(gè)目錄跟傳入的參數(shù)匹配,所以這個(gè)集合里面只有一個(gè)元素
NSString *documents = [array objectAtIndex:0];
tmp:NSString *tmp = NSTemporaryDirectory();
Library/Caches:(跟Documents類(lèi)似的2種?方法)
利用沙盒根目錄拼接”Caches”字符串
利?NSSearchPathForDirectoriesInDomains函數(shù)(將函數(shù)的第2個(gè)參數(shù)改 為:NSCachesDirectory即可)
Library/Preference:通過(guò)NSUserDefaults類(lèi)存取該目錄下的設(shè)置信息
相應(yīng)的代碼:
<u>復(fù)制代碼</u>代碼如下:
import "NJViewController.h"
import "NJPerson.h"
@interface NJViewController ()
- (IBAction)saveDataBtnClick:(id)sender;
- (IBAction)readDataBtnClick:(id)sender;
@end
<u>復(fù)制代碼</u>代碼如下:
@implementation NJViewController
/**
- 點(diǎn)擊保存按鈕
*/
-
(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();// 不建議寫(xiě)/
//NSString *path = [home stringByAppendingString:@"/Documents"];
// 不建議Documents寫(xiě)死
//NSString *path = [home stringByAppendingPathComponent:@"Documents"];// NSUserDomainMask 在用戶目錄下查找
// YES 代表用戶目錄的~
// NSDocumentDirectory 查找Documents文件夾
// 建議使用如下方法動(dòng)態(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ù)寫(xiě)入文件
// [dict writeToFile:path atomically:YES];/*
plist只能存儲(chǔ)系統(tǒng)自帶的一些常規(guī)的類(lèi), 也就是有writeToFile方法的對(duì)象才可以使用plist保存數(shù)據(jù)
字符串/字典/數(shù)據(jù)/NSNumber/NSData ...
*/// 自定義的對(duì)象不能保存到plist中
NJPerson *p = [[NJPerson alloc] init];
p.name =@"lnj";NSDictionary dict = @{@"person": @"abc"};
[dict writeToFile:path atomically:YES];
}
/*
- 點(diǎn)擊讀取按鈕
*/
-
(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
如果對(duì)象是NSString汁咏、NSDictionary亚斋、NSArray、NSData攘滩、 NSNumber等類(lèi)型,就可以使用writeToFile:atomically:?法 直接將對(duì)象寫(xiě)到屬性列表文件中
NSKeydeArchiver歸檔
一帅刊、簡(jiǎn)單說(shuō)明
在使用plist進(jìn)行數(shù)據(jù)存儲(chǔ)和讀取,只適用于系統(tǒng)自帶的一些常用類(lèi)型才能用漂问,且必須先獲取路徑相對(duì)麻煩赖瞒;
偏好設(shè)置(將所有的東西都保存在同一個(gè)文件夾下面,且主要用于存儲(chǔ)應(yīng)用的設(shè)置信息)
歸檔:因?yàn)榍皟烧叨加幸粋€(gè)致命的缺陷蚤假,只能存儲(chǔ)常用的類(lèi)型栏饮。歸檔可以實(shí)現(xiàn)把自定義的對(duì)象存放在文件中。
二勤哗、代碼示例
1.文件結(jié)構(gòu)
<u>復(fù)制代碼</u>代碼如下:
//
// 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
<u>復(fù)制代碼</u>代碼如下:
@implementation YYViewController
(void)viewDidLoad
{
[super viewDidLoad];
}-
(IBAction)saveBtnOnclick:(id)sender {
//1.創(chuàng)建對(duì)象
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.將自定義的對(duì)象保存到文件中
[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.從文件中讀取對(duì)象
YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
}
@end
新建一個(gè)person類(lèi)
YYPerson.h文件
<u>復(fù)制代碼</u>代碼如下:
//
// YYPerson.h
// 02-歸檔
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
//
import <Foundation/Foundation.h>
// 如果想將一個(gè)自定義對(duì)象保存到文件中必須實(shí)現(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文件
<u>復(fù)制代碼</u>代碼如下:
//
// YYPerson.m
// 02-歸檔
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
//
import "YYPerson.h"
@implementation YYPerson
// 當(dāng)將一個(gè)自定義對(duì)象保存到文件的時(shí)候就會(huì)調(diào)用該方法
// 在該方法中說(shuō)明如何存儲(chǔ)自定義對(duì)象的屬性
// 也就說(shuō)在該方法中說(shuō)清楚存儲(chǔ)自定義對(duì)象的哪些屬性
-(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"];
}
// 當(dāng)從文件中讀取一個(gè)對(duì)象的時(shí)候就會(huì)調(diào)用該方法
// 在該方法中說(shuō)明如何讀取保存在文件中的對(duì)象
// 也就是說(shuō)在該方法中說(shuō)清楚怎么讀取文件中的對(duì)象
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"調(diào)用了initWithCoder:方法");
//注意:在構(gòu)造方法中需要先初始化父類(lèi)的方法
if (self=[super init]) {
self.name=[aDecoder decodeObjectForKey:@"name"];
self.age=[aDecoder decodeIntegerForKey:@"age"];
self.height=[aDecoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
3.打印效果和兩個(gè)重要的錯(cuò)誤提示
點(diǎn)擊保存按鈕和讀取按鈕抡爹,成功打印結(jié)果如下:
關(guān)于不實(shí)現(xiàn)兩個(gè)協(xié)議方法的錯(cuò)誤提示:
-(void)encodeWithCoder:(NSCoder *)aCoder方法:
-(id)initWithCoder:(NSCoder *)aDecoder方法:
三、繼承類(lèi)中的使用
新建一個(gè)學(xué)生類(lèi)芒划,讓這個(gè)類(lèi)繼承自Preson這個(gè)類(lèi)冬竟,增加一個(gè)體重的屬性。
YYstudent.h文件
<u>復(fù)制代碼</u>代碼如下:
//
// YYstudent.h
// 02-歸檔
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
//
import "YYPerson.h"
@interface YYstudent : YYPerson
//增加一個(gè)體重屬性
@property(nonatomic,assign) double weight;
@end
YYstudent.m文件
<u>復(fù)制代碼</u>代碼如下:
//
// YYstudent.m
// 02-歸檔
//
// Created by apple on 14-6-7.
// Copyright (c) 2014年 itcase. All rights reserved.
//
import "YYstudent.h"
@implementation YYstudent
//在子類(lèi)中重寫(xiě)這兩個(gè)方法
(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文件
<u>復(fù)制代碼</u>代碼如下:
//
// 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
<u>復(fù)制代碼</u>代碼如下:
@implementation YYViewController
(void)viewDidLoad
{
[super viewDidLoad];
}-
(IBAction)saveBtnOnclick:(id)sender {
//1.創(chuàng)建對(duì)象
// 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.將自定義的對(duì)象保存到文件中
// [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.從文件中讀取對(duì)象
// 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
點(diǎn)擊保存按鈕和讀取按鈕后的打印輸出:
四民逼、重要說(shuō)明
1.保存數(shù)據(jù)過(guò)程:
<u>復(fù)制代碼</u>代碼如下:
//1.創(chuàng)建對(duì)象
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.將自定義的對(duì)象保存到文件中
[NSKeyedArchiver archiveRootObject:s toFile:path];
2.讀取數(shù)據(jù)過(guò)程:
<u>復(fù)制代碼</u>代碼如下:
//1.獲取文件路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
//2.從文件中讀取對(duì)象
YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
3.遵守NSCoding協(xié)議泵殴,并實(shí)現(xiàn)該協(xié)議中的兩個(gè)方法。
4.如果是繼承拼苍,則子類(lèi)一定要重寫(xiě)那兩個(gè)方法笑诅。因?yàn)閜erson的子類(lèi)在存取的時(shí)候,會(huì)去子類(lèi)中去找調(diào)用的方法疮鲫,沒(méi)找到那么它就去父類(lèi)中找吆你,所以最后保存和讀取的時(shí)候新增加的屬性會(huì)被忽略。需要先調(diào)用父類(lèi)的方法俊犯,先初始化父類(lèi)的妇多,再初始化子類(lèi)的。
5.保存數(shù)據(jù)的文件的后綴名可以隨意命名燕侠。
6.通過(guò)plist保存的數(shù)據(jù)是直接顯示的者祖,不安全立莉。通過(guò)歸檔方法保存的數(shù)據(jù)在文件中打開(kāi)是亂碼的,更安全七问。
轉(zhuǎn)自:http://www.jb51.net/article/76218.htm