国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

iOS各種系統(tǒng)文件目錄臨時,緩存,document,lib,歸檔,序列化

2019-11-14 19:46:34
字體:
供稿:網(wǎng)友
java代碼 復制代碼 收藏代碼
  1. /** 
  2.     1:Documents:應用中用戶數(shù)據(jù)可以放在這里,iTunes備份和恢復的時候會包括此目錄 
  3.     2:tmp:存放臨時文件,iTunes不會備份和恢復此目錄,此目錄下文件可能會在應用退出后刪除 
  4.     3:Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應用退出刪除 
  5.      */  
  6.     NSArray *paths1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory  
  7.                                                        , NSUserDomainMask  
  8.                                                        , YES);  
  9.       
  10.       
  11.     NSString *documentsDirect=[paths1 objectAtIndex:0];  
  12.     assert(1 == paths1.count);  
  13.     NSLog(@">>documentsDirect=%@",documentsDirect);  
  14.    
  15.     NSArray *Librarypaths =  NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSDocumentDirectory, YES);  
  16.     NSString* libraryDirectory  = [Librarypaths objectAtIndex:0];  
  17.     NSLog(@">>Librarypaths.length =%d",[Librarypaths count]);  
  18.      assert(1 < Librarypaths.count);  
  19.        
  20.     NSLog(@"libraryDirectory=%@",libraryDirectory);  
  21.       
  22.     //如果要指定其他文件目錄,比如Caches目錄,需要更換目錄工廠常量,上面代碼其他的可不變  
  23.     NSArray *pathcaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory  
  24.                                                        , NSUserDomainMask  
  25.                                                        , YES);  
  26.     NSString* cacheDirectory  = [pathcaches objectAtIndex:0];  
  27.     NSLog(@"cacheDirectory=%@",cacheDirectory);  
  28.     /** 
  29.      使用NSSearchPathForDirectoriesInDomains只能定位Caches目錄和Documents目錄。 
  30.       
  31.      tmp目錄,不能按照上面的做法獲得目錄了,有個函數(shù)可以獲得應用的根目錄 
  32.      */  
  33.     NSString *tempDir1=NSHomeDirectory() ;  
  34.     NSString *tempDir2=NSTemporaryDirectory();  
  35.     NSLog(@"tempDir1=%@",tempDir1);  
  36.     NSLog(@"tempDir2=%@",tempDir2);  
/**    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)換

 

 2.1 歸檔

 如果我們需要將自定義的一個對象保存到文件,應該如何做呢? 

這里引入兩個東西:一個是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類型的屬性

Java代碼 復制代碼 收藏代碼
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Address : NSObject<NSCoding>  
  4. {  
  5.     NSString *country;  
  6.     NSString *city;  
  7. }  
  8. @PRoperty(nonatomic,copy) NSString *country;  
  9. @property(nonatomic,copy) NSString *city;   
  10. @end  
#import <Foundation/Foundation.h>@interface Address : NSObject<NSCoding>{    NSString *country;    NSString *city;}@property(nonatomic,copy) NSString *country;@property(nonatomic,copy) NSString *city; @end

 

Java代碼 復制代碼 收藏代碼
  1. #import "Address.h"  
  2.   
  3. @implementation Address  
  4. @synthesize country;  
  5. @synthesize city;  
  6. - (void)encodeWithCoder:(NSCoder *)aCoder{  
  7.     [aCoder encodeObject:country forKey:@"country"];  
  8.     [aCoder encodeObject:city forKey:@"city"];  
  9. }  
  10. - (id)initWithCoder:(NSCoder *)aDecoder{  
  11.     if (self = [super init]) {  
  12.         [self setCountry:[aDecoder decodeObjectForKey:@"country"]];  
  13.         [self setCity:[aDecoder decodeObjectForKey:@"city"]];  
  14.     } return self;  
  15. }  
  16. @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

 

Java代碼 復制代碼 收藏代碼
  1. #import <Foundation/Foundation.h>  
  2. #import "Address.h"  
  3. @interface User : NSObject<NSCoding>{  
  4.     NSString *_name;  
  5.     NSString *_passWord;  
  6.     Address *_address;  
  7. }  
  8. @property(nonatomic,copy) NSString *name;  
  9. @property(nonatomic,copy) NSString *password;  
  10. @property(nonatomic,retain) Address *address;  
  11.   
  12. @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

 

Java代碼 復制代碼 收藏代碼
  1. #import "User.h"  
  2.   
  3.   
  4. @implementation User  
  5. @synthesize name = _name;  
  6. @synthesize password = _password;  
  7. @synthesize address = _address;  
  8.   
  9. - (void)encodeWithCoder:(NSCoder *)aCoder{  
  10.     [aCoder encodeObject:_name forKey:@"name"];  
  11.     [aCoder encodeObject:_password forKey:@"password"];  
  12.     [aCoder encodeObject:_address forKey:@"address"];  
  13. }  
  14. - (id)initWithCoder:(NSCoder *)aDecoder{  
  15.     if (self = [super init]) {  
  16.         [self setName:[aDecoder decodeObjectForKey:@"name"]];  
  17.         [self setPassword:[aDecoder decodeObjectForKey:@"password"]];  
  18.         [self setAddress:[aDecoder decodeObjectForKey:@"address"]];  
  19.     }  
  20.     return self;  
  21. }  
  22.   
  23. @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

 操作應用

Java代碼 復制代碼 收藏代碼
  1. NSString *tempDir2=NSTemporaryDirectory();      
  2.      
  3.      
  4. // Do any additional setup after loading the view, typically from a nib.  
  5.    Address *myAddress = [[Address alloc] init] ;  
  6.    myAddress.country = @"中國";  
  7.    myAddress.city = @"杭州";  
  8.    User *user = [[User alloc] init] ;  
  9.      
  10.    user.name = @"盧克";  
  11.    user.password = @"lukejin";  
  12.    user.address = myAddress;  
  13.    //歸檔  保存的是plist的二進制數(shù)據(jù)格式  
  14.    NSString *path = [tempDir2 stringByAppendingPathComponent:@"user"];  
  15.    [NSKeyedArchiver archiveRootObject:user toFile:path];  
  16.      
  17.      
  18.    //從文檔中讀取  
  19.    User *object = [NSKeyedUnarchiver unarchiveObjectWithFile:path];  
  20.    NSLog(@"object.name : %@",object.name);  
 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ù)對象自帶的方法,如字典類寫文件:

數(shù)據(jù):

Java代碼 復制代碼 收藏代碼
  1. NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init] ;  
  2.   [dataDictionary setValue:[NSNumber numberWithInt:222] forKey:@"intNumber"];  
  3.   [dataDictionary setValue:[NSArray arrayWithObjects:@"1",@"2", nil] forKey:@"testArray"];  
  NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] init] ;    [dataDictionary setValue:[NSNumber numberWithInt:222] forKey:@"intNumber"];    [dataDictionary setValue:[NSArray arrayWithObjects:@"1",@"2", nil] forKey:@"testArray"];

 寫文件

Java代碼 復制代碼 收藏代碼
  1. [dataDictionary writeToFile:@"/Users/zhoumoban/Desktop/test.plist" atomically:YES];  
[dataDictionary writeToFile:@"/Users/zhoumoban/Desktop/test.plist" atomically:YES];

 讀文件:

Java代碼 復制代碼 收藏代碼
  1. NSDictionary *dictionaryFromFile = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhoumoban/Desktop/test.plist"];  
  2.    NSLog(@"%@",[dictionaryFromFile objectForKey:@"intNumber"]);  
 NSDictionary *dictionaryFromFile = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhoumoban/Desktop/test.plist"];    NSLog(@"%@",[dictionaryFromFile objectForKey:@"intNumber"]);

 另外:使用NSPropertyListSerialization類。通過NSPropertyListSerialization類可以將數(shù)據(jù)對象直接轉(zhuǎn)成NSData或者直接寫到文件或者流中去

Java代碼 復制代碼 收藏代碼
  1. NSString *error;  
  2.       
  3.     NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:dataDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];  
  4.       
  5.     if(xmlData) {  
  6.         NSLog(@"No error creating XML data.");  
  7.         [xmlData writeToFile:@"/Users/zhoumoban/Desktop/test2.plist" atomically:YES];  
  8.     } else {  
  9.         if (error) {  
  10.             NSLog(@"error:%@", error);  
  11.            // [error release];  
  12.         }  
  13.     }  
  14.      //讀取  
  15.     NSDictionary *dictionaryFromFile2 = (NSDictionary *)[NSPropertyListSerialization propertyListWithData:[NSData dataWithContentsOfFile:@"/Users/zhoumoban/Desktop/test2.plist"] options:0 format:NULL error:&error];  
  16.     NSLog(@"===%@",[dictionaryFromFile2 objectForKey:@"intNumber"]);  

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄯善县| 金塔县| 沙坪坝区| 芜湖市| 宁强县| 江都市| 宁波市| 齐齐哈尔市| 农安县| 临猗县| 贡山| 隆回县| 大丰市| 都安| 渑池县| 阜康市| 曲阳县| 金溪县| 兴仁县| 静安区| 云龙县| 论坛| 新民市| 铜山县| 沁源县| 柘荣县| 中山市| 昌宁县| 汾阳市| 仁怀市| 唐河县| 驻马店市| 绥滨县| 青龙| 蒙山县| 安吉县| 湖口县| 夏津县| 中宁县| 南京市| 衡水市|