xml屬性列表(plist)歸檔
PReference(偏好設(shè)置)
NSKeyedArchiver歸檔(NSCoding)
SQLite3
CoreData
每個(gè)iOS應(yīng)用都有自己的應(yīng)用沙盒(應(yīng)用沙盒就是文件系統(tǒng)目錄),與其他文件系統(tǒng)隔離。其他應(yīng)用不能訪問(wèn)沙盒中的內(nèi)容。
模擬器應(yīng)用沙盒的根路徑在: (apple是用戶名, 6.0是模擬器版本)
/Users/apple/Library/applicationSupport/iphone Simulator/6.0/Applications
沙盒根目錄:NSString *home= NSHomeDirectory();
Documents:(2種方式)
1 NSString *home = NSHomeDirectory();2 NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
1 //NSUserDomainMask 代表從用戶文件夾下找2 // YES 代表展開(kāi)路徑中的波浪字符“~”3 NSArray*array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, NO);4 // 在iOS中,只有一個(gè)目錄跟傳入的參數(shù)匹配,所以這個(gè)集合里面只有一個(gè)元素5 NSString*documents = [array objectAtIndex:0];
tmp:NSString *tmp = NSTemporaryDirectory();
Library/Caches:(跟Documents類似的2種方法)
Library/Preference:通過(guò)NSUserDefaults類存取該目錄下的設(shè)置信息
屬性列表是一種XML格式的文件,拓展名為plist
如果對(duì)象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對(duì)象寫到屬性列表文件中
將一個(gè)NSDictionary對(duì)象歸檔到一個(gè)plist屬性列表中:
1 // 將數(shù)據(jù)封裝成字典2 NSMutableDictionary *dict =[NSMutableDictionary dictionary];3 [dict setObject:@"母雞"forKey:@"name"];4 [dict setObject:@"15013141314"forKey:@"phone"];5 [dict setObject:@"27" forKey:@"age"];6 7 // 將字典持久化到Documents/stu.plist文件中8 [dict writeToFile:path atomically:YES];
讀取屬性列表,恢復(fù)NSDictionary對(duì)象:
1 // 讀取Documents/stu.plist的內(nèi)容,實(shí)例化NSDictionary2 NSDictionary*dict = [NSDictionary dictionaryWithContentsOfFile:path];3 NSLog(@"name:%@",[dict objectForKey:@"name"]);4 NSLog(@"phone:%@",[dict objectForKey:@"phone"]);5 NSLog(@"age:%@",[dict objectForKey:@"age"]);
很多iOS應(yīng)用都支持偏好設(shè)置,比如保存用戶名、密碼、字體大小等設(shè)置,iOS提供了一套標(biāo)準(zhǔn)的解決方案來(lái)為應(yīng)用加入偏好設(shè)置功能。
每個(gè)應(yīng)用都有個(gè)NSUserDefaults實(shí)例,通過(guò)它來(lái)存取偏好設(shè)置。比如,保存用戶名、字體大小、是否自動(dòng)登錄。
1 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];2 [defaultssetObject:@"zhangsan" forKey:@"username"];3 [defaultssetFloat:18.0f forKey:@"text_size"];4 [defaultssetBool:YES forKey:@"auto_login"];
讀取上次保存的設(shè)置
1 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];2 NSString *username = [defaults stringForKey:@"username"];3 floattextSize = [defaults floatForKey:@"text_size"];4 BOOLautoLogin = [defaults boolForKey:@"auto_login"];
注意:UserDefaults設(shè)置數(shù)據(jù)時(shí),不是立即寫入,而是根據(jù)時(shí)間戳定時(shí)地把緩存中的數(shù)據(jù)寫入本地磁盤。所以調(diào)用了set方法之后數(shù)據(jù)有可能還沒(méi)有寫入磁盤應(yīng)用程序就終止了。出現(xiàn)以上問(wèn)題,可以通過(guò)調(diào)用synchornize方法強(qiáng)制寫入
1 [defaults synchornize];如果對(duì)象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,可以直接用NSKeyedArchiver進(jìn)行歸檔和恢復(fù)。
不是所有的對(duì)象都可以直接用這種方法進(jìn)行歸檔,只有遵守了NSCoding協(xié)議的對(duì)象才可以。
NSCoding協(xié)議有2個(gè)方法:
1 // 歸檔一個(gè)NSArray對(duì)象到Documents/array.archive2 NSArray *array = [NSArray arrayWithObjects:@”a”,@”b”,nil];3 [NSKeyedArchiverarchiveRootObject:array toFile:path];
恢復(fù)(解碼)NSArray對(duì)象
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
Person.h
1 @interfacePerson : NSObject<NSCoding>2 @property(nonatomic, copy) NSString *name;3 @property(nonatomic, assign) int age;4 @property(nonatomic, assign) float height;5 @end
Person.m
1 @implementation : Person 2 // 在這個(gè)方法中聲明需要存儲(chǔ)的屬性 3 -(void)encodeWithCoder:(NSCoder *)encoder { 4 [super encodeWithCoder:encoder]; 5 [encoder encodeObject:self.name forKey:@"name"]; 6 [encoder encodeInt:self.age forKey:@"age"]; 7 [encoder encodeFloat:self.height forKey:@"height"]; 8 } 9 10 // 構(gòu)造方法,當(dāng)初始化對(duì)象時(shí)會(huì)先調(diào)該方法11 -(id)initWithCoder:(NSCoder *)decoder {12 if (self = [super initWithCoder:decoder]) {13 self.name = [decoder decodeObjectForKey:@"name"];14 self.age = [decoder decodeIntForKey:@"age"];15 self.height = [decoder decodeFloatForKey:@"height"];16 }17 return self;18 }19 @end
歸檔
1 Person *person = [[Person alloc] init];2 person.name= @"zhangsan";3 person.age= 18;4 person.height= 1.73f;5 6 [NSKeyedArchiverarchiveRootObject:person toFile:path];
恢復(fù)
1 Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];如果父類也遵守了NSCoding協(xié)議,請(qǐng)注意:
應(yīng)該在encodeWithCoder:方法中加上一句[super encodeWithCode:encode];確保繼承的實(shí)例變量也能被編碼,即也能被歸檔
應(yīng)該在initWithCoder:方法中加上一句self = [super initWithCoder:decoder];確保繼承的實(shí)例變量也能被解碼,即也能被恢復(fù)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注