/** 1:Documents:應用中用戶數(shù)據(jù)可以放在這里,iTunes備份和恢復的時候會包括此目錄 2:tmp:存放臨時文件,iTunes不會備份和恢復此目錄,此目錄下文件可能會在應用退出后刪除 3:Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除 */ NSArray *paths1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask , YES); NSString *documentsDirect=[paths1 objectAtIndex:0]; assert(1 == paths1.count); NSLog(@">>documentsDirect=%@",documentsDirect); NSArray *Librarypaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSDocumentDirectory, YES); NSString* libraryDirectory = [Librarypaths objectAtIndex:0]; NSLog(@">>Librarypaths.length =%d",[Librarypaths count]); assert(1 < Librarypaths.count); NSLog(@"libraryDirectory=%@",libraryDirectory); //如果要指定其他文件目錄,比如Caches目錄,需要更換目錄工廠常量,上面代碼其他的可不變 NSArray *pathcaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory , NSUserDomainMask , YES); NSString* cacheDirectory = [pathcaches objectAtIndex:0]; NSLog(@"cacheDirectory=%@",cacheDirectory); /** 使用NSSearchPathForDirectoriesInDomains只能定位Caches目錄和Documents目錄。 tmp目錄,不能按照上面的做法獲得目錄了,有個函數(shù)可以獲得應用的根目錄 */ NSString *tempDir1=NSHomeDirectory() ; NSString *tempDir2=NSTemporaryDirectory(); NSLog(@"tempDir1=%@",tempDir1); NSLog(@"tempDir2=%@",tempDir2);
歸檔 普通自定義對象和字節(jié)流之間的轉(zhuǎn)換
序列化 某些特定類型(NSDictionary, NSArray, NSString, NSDate, NSNumber,NSData)的數(shù)據(jù)和字節(jié)流之間(通常將其保存為plist文件)的轉(zhuǎn)換
如果我們需要將自定義的一個對象保存到文件,應該如何做呢?
這里引入兩個東西:一個是NSCoding協(xié)議 ;另一個是NSKeyedArchiver,NSKeyedArchiver其實繼承于NSCoder,可以以鍵值對的方式將對象的屬性進行序列化和反序列化。
具體的過程可以這樣描述 通過NSKeyedArchiver 可以將實現(xiàn)了NSCoding協(xié)議的對象 和 字節(jié)流 相互轉(zhuǎn)換 。
像一些框架中的數(shù)據(jù)類型如NSDictionary,NSArray,NSString... 都已經(jīng)實現(xiàn)了NSCoding協(xié)議,所以可以直接對他們進行歸檔操作。
這里來一個比較完整的例子,一個Address類,一個User類,User類下有個Address類型的屬性
#import <Foundation/Foundation.h>@interface Address : NSObject<NSCoding>{ NSString *country; NSString *city;}@property(nonatomic,copy) NSString *country;@property(nonatomic,copy) NSString *city; @end
#import "Address.h"@implementation Address@synthesize country;@synthesize city;- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:country forKey:@"country"]; [aCoder encodeObject:city forKey:@"city"];}- (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { [self setCountry:[aDecoder decodeObjectForKey:@"country"]]; [self setCity:[aDecoder decodeObjectForKey:@"city"]]; } return self;}@end
#import <Foundation/Foundation.h>#import "Address.h"@interface User : NSObject<NSCoding>{ NSString *_name; NSString *_password; Address *_address;}@property(nonatomic,copy) NSString *name;@property(nonatomic,copy) NSString *password;@property(nonatomic,retain) Address *address;@end
#import "User.h"@implementation User@synthesize name = _name;@synthesize password = _password;@synthesize address = _address;- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:_name forKey:@"name"]; [aCoder encodeObject:_password forKey:@"password"]; [aCoder encodeObject:_address forKey:@"address"];}- (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { [self setName:[aDecoder decodeObjectForKey:@"name"]]; [self setPassword:[aDecoder decodeObjectForKey:@"password"]]; [self setAddress:[aDecoder decodeObjectForKey:@"address"]]; } return self;}@end操作應用
NSString *tempDir2=NSTemporaryDirectory(); // Do any additional setup after loading the view, typically from a nib. Address *myAddress = [[Address alloc] init] ; myAddress.country = @"中國"; myAddress.city = @"杭州"; User *user = [[User alloc] init] ; user.name = @"盧克"; user.password = @"lukejin"; user.address = myAddress; //歸檔 保存的是plist的二進制數(shù)據(jù)格式 NSString *path = [tempDir2 stringByAppendingPathComponent:@"user"]; [NSKeyedArchiver archiveRootObject:user toFile:path]; //從文檔中讀取 User *object = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; NSLog(@"object.name : %@",object.name);
數(shù)據(jù):
NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init] ; [dataDictionary setValue:[NSNumber numberWithInt:222] forKey:@"intNumber"]; [dataDictionary setValue:[NSArray arrayWithObjects:@"1",@"2", nil] forKey:@"testArray"];
寫文件
[dataDictionary writeToFile:@"/Users/zhoumoban/Desktop/test.plist" atomically:YES];
讀文件:
NSDictionary *dictionaryFromFile = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhoumoban/Desktop/test.plist"]; NSLog(@"%@",[dictionaryFromFile objectForKey:@"intNumber"]);
另外:使用NSPropertyListSerialization類。通過NSPropertyListSerialization類可以將數(shù)據(jù)對象直接轉(zhuǎn)成NSData或者直接寫到文件或者流中去
新聞熱點
疑難解答