xml屬性列表(plist)歸檔
PReference(偏好設置)
NSKeyedArchiver歸檔(NSCoding)
SQLite3
CoreData
每個iOS應用都有自己的應用沙盒(應用沙盒就是文件系統目錄),與其他文件系統隔離。其他應用不能訪問沙盒中的內容。
模擬器應用沙盒的根路徑在: (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 代表展開路徑中的波浪字符“~”3 NSArray*array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, NO);4 // 在iOS中,只有一個目錄跟傳入的參數匹配,所以這個集合里面只有一個元素5 NSString*documents = [array objectAtIndex:0];
tmp:NSString *tmp = NSTemporaryDirectory();
Library/Caches:(跟Documents類似的2種方法)
Library/Preference:通過NSUserDefaults類存取該目錄下的設置信息
屬性列表是一種XML格式的文件,拓展名為plist
如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中
將一個NSDictionary對象歸檔到一個plist屬性列表中:
1 // 將數據封裝成字典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];
讀取屬性列表,恢復NSDictionary對象:
1 // 讀取Documents/stu.plist的內容,實例化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應用都支持偏好設置,比如保存用戶名、密碼、字體大小等設置,iOS提供了一套標準的解決方案來為應用加入偏好設置功能。
每個應用都有個NSUserDefaults實例,通過它來存取偏好設置。比如,保存用戶名、字體大小、是否自動登錄。
1 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];2 [defaultssetObject:@"zhangsan" forKey:@"username"];3 [defaultssetFloat:18.0f forKey:@"text_size"];4 [defaultssetBool:YES forKey:@"auto_login"];
讀取上次保存的設置
1 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];2 NSString *username = [defaults stringForKey:@"username"];3 floattextSize = [defaults floatForKey:@"text_size"];4 BOOLautoLogin = [defaults boolForKey:@"auto_login"];
注意:UserDefaults設置數據時,不是立即寫入,而是根據時間戳定時地把緩存中的數據寫入本地磁盤。所以調用了set方法之后數據有可能還沒有寫入磁盤應用程序就終止了。出現以上問題,可以通過調用synchornize方法強制寫入
1 [defaults synchornize];
如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,可以直接用NSKeyedArchiver進行歸檔和恢復。
不是所有的對象都可以直接用這種方法進行歸檔,只有遵守了NSCoding協議的對象才可以。
NSCoding協議有2個方法:
1 // 歸檔一個NSArray對象到Documents/array.archive2 NSArray *array = [NSArray arrayWithObjects:@”a”,@”b”,nil];3 [NSKeyedArchiverarchiveRootObject:array toFile:path];
恢復(解碼)NSArray對象
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 // 在這個方法中聲明需要存儲的屬性 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 // 構造方法,當初始化對象時會先調該方法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];
恢復
1 Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
如果父類也遵守了NSCoding協議,請注意:
應該在encodeWithCoder:方法中加上一句[super encodeWithCode:encode];確保繼承的實例變量也能被編碼,即也能被歸檔
應該在initWithCoder:方法中加上一句self = [super initWithCoder:decoder];確保繼承的實例變量也能被解碼,即也能被恢復
新聞熱點
疑難解答