UI学习笔记---第十四天数据持久化
一.沙盒機制
每個應用程序位于文件系統(tǒng)的嚴格限制部分
每個應用程序只能在為該程序創(chuàng)建的文件系統(tǒng)中讀取文件
每個應用程序在iOS系統(tǒng)內斗放在了統(tǒng)一的文件夾目錄下
沙盤路徑的位置
1. 通過Finder查找程序沙盤相對路徑
~/Library/Application Support/iPhone Simulator
2. 通過代碼查找程序沙盤相對路徑
NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory,NSSearchPathDomainMask domainMask,BOOL expandTilde);?
常見問題:
模擬器路徑內有可能包含多個系統(tǒng)版本的路徑
NSDocumentDirectory————>Documents文件夾
NSDocumentationDirectory——>Documentation文件夾 ? //不屬于iOS ?是Mac-os x中使用
NSTemporaryDirectory()———>tmp文件夾
NSLibraryDirectory——————>Library文件夾?
二.簡單對象寫入文件
文件存儲的相對目錄
//獲取所有的documents文件夾
NSArray *documentList =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);?
//將最后一個documents文件夾路徑作為問價的存儲目錄
NSString *documentsPath = documentList.lastObject;
字符串對象寫入文件
數(shù)組對象寫入文件
字典對象寫入文件
二進制對象寫入文件
三.復雜對象寫入文件
什么是復雜對象
1.在Foundation框架內不存在的數(shù)據(jù)類
2.無法在程序內通過writeToFile類型的方法寫入到文件內
3.復雜對象至少包含一個實例對象
?
構造復雜對象的生成類
@interface ABPersonInfo : NSObject
@property (nonatomic ,retain) NSString *name;
@property (nonatomic ,retain) NSNumber *age;
@end
@implementation ABPersonInfo
@end?
NSCoding協(xié)議
@protocol NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end?
?
使復雜對象接受NSCoding協(xié)議
@interface ABPersonInfo:NSObject<NSCoding>
使復雜對象實現(xiàn)NSCoding協(xié)議
@implementation ABPersonInfo
- (id)initWithCoder:(NSCoder *)aDecoder{
? ? self = [super init]; if (self) {
? ? ? ? self.age = [aDecoder decodeObjectForKey:@"age"];
? ? ? ? self.name = [aDecoder decodeObjectForKey:@"name"]; }
? ? return self; }
- (void)encodeWithCoder:(NSCoder *)aCoder{
? ? [aCoder encodeObject:self.age forKey:@"age"]; [aCoder encodeObject:self.name forKey:@"name"];
}
@end
將復雜對象ABPersonInfo的實例對象寫入文件
?
轉載于:https://www.cnblogs.com/limicheng/p/3876673.html
總結
以上是生活随笔為你收集整理的UI学习笔记---第十四天数据持久化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ALAssetsLibrary获取相册列
- 下一篇: oracle入门(8)——实战:支持可变