日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

iOS 各种系统文件目录 临时,缓存,document,lib,归档,序列化

發(fā)布時(shí)間:2025/3/17 windows 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 各种系统文件目录 临时,缓存,document,lib,归档,序列化 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java代碼 ?
  • /**?
  • ????1:Documents:應(yīng)用中用戶數(shù)據(jù)可以放在這里,iTunes備份和恢復(fù)的時(shí)候會包括此目錄?
  • ????2:tmp:存放臨時(shí)文件,iTunes不會備份和恢復(fù)此目錄,此目錄下文件可能會在應(yīng)用退出后刪除?
  • ????3:Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應(yīng)用退出刪除?
  • ?????*/??
  • ????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目錄,不能按照上面的做法獲得目錄了,有個(gè)函數(shù)可以獲得應(yīng)用的根目錄?
  • ?????*/??
  • ????NSString?*tempDir1=NSHomeDirectory()?;??
  • ????NSString?*tempDir2=NSTemporaryDirectory();??
  • ????NSLog(@"tempDir1=%@",tempDir1);??
  • ????NSLog(@"tempDir2=%@",tempDir2);??
  • /**1:Documents:應(yīng)用中用戶數(shù)據(jù)可以放在這里,iTunes備份和恢復(fù)的時(shí)候會包括此目錄2:tmp:存放臨時(shí)文件,iTunes不會備份和恢復(fù)此目錄,此目錄下文件可能會在應(yīng)用退出后刪除3:Library/Caches:存放緩存文件,iTunes不會備份此目錄,此目錄下文件不會在應(yīng)用退出刪除*/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目錄,不能按照上面的做法獲得目錄了,有個(gè)函數(shù)可以獲得應(yīng)用的根目錄*/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 歸檔

    ?如果我們需要將自定義的一個(gè)對象保存到文件,應(yīng)該如何做呢??

    這里引入兩個(gè)東西:一個(gè)是NSCoding協(xié)議 ;另一個(gè)是NSKeyedArchiver,NSKeyedArchiver其實(shí)繼承于NSCoder,可以以鍵值對的方式將對象的屬性進(jìn)行序列化和反序列化。?
    具體的過程可以這樣描述?通過NSKeyedArchiver 可以將實(shí)現(xiàn)了NSCoding協(xié)議的對象 和 字節(jié)流 相互轉(zhuǎn)換?。

    像一些框架中的數(shù)據(jù)類型如NSDictionary,NSArray,NSString... 都已經(jīng)實(shí)現(xiàn)了NSCoding協(xié)議,所以可以直接對他們進(jìn)行歸檔操作。

    ??這里來一個(gè)比較完整的例子,一個(gè)Address類,一個(gè)User類,User類下有個(gè)Address類型的屬性

    Java代碼 ?
  • #import?<Foundation/Foundation.h>??
  • ??
  • @interface?Address?:?NSObject<NSCoding>??
  • {??
  • ????NSString?*country;??
  • ????NSString?*city;??
  • }??
  • @property(nonatomic,copy)?NSString?*country;??
  • @property(nonatomic,copy)?NSString?*city;???
  • @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代碼 ?
  • #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 "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代碼 ?
  • #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 <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代碼 ?
  • #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??
  • #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

    ?操作應(yīng)用

    Java代碼 ?
  • 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的二進(jìn)制數(shù)據(jù)格式??
  • ???NSString?*path?=?[tempDir2?stringByAppendingPathComponent:@"user"];??
  • ???[NSKeyedArchiver?archiveRootObject:user?toFile:path];??
  • ?????
  • ?????
  • ???//從文檔中讀取??
  • ???User?*object?=?[NSKeyedUnarchiver?unarchiveObjectWithFile:path];??
  • ???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的二進(jìn)制數(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代碼 ?
  • NSMutableDictionary?*dataDictionary?=?[[NSMutableDictionary?alloc]?init]?;??
  • ??[dataDictionary?setValue:[NSNumber?numberWithInt:222]?forKey:@"intNumber"];??
  • ??[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代碼 ?
  • [dataDictionary?writeToFile:@"/Users/zhoumoban/Desktop/test.plist"?atomically:YES];??
  • [dataDictionary writeToFile:@"/Users/zhoumoban/Desktop/test.plist" atomically:YES];

    ?讀文件:

    Java代碼 ?
  • NSDictionary?*dictionaryFromFile?=?[NSDictionary?dictionaryWithContentsOfFile:@"/Users/zhoumoban/Desktop/test.plist"];??
  • ???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代碼 ?
  • NSString?*error;??
  • ??????
  • ????NSData?*xmlData?=?[NSPropertyListSerialization?dataFromPropertyList:dataDictionary?format:NSPropertyListXMLFormat_v1_0?errorDescription:&error];??
  • ??????
  • ????if(xmlData)?{??
  • ????????NSLog(@"No?error?creating?XML?data.");??
  • ????????[xmlData?writeToFile:@"/Users/zhoumoban/Desktop/test2.plist"?atomically:YES];??
  • ????}?else?{??
  • ????????if?(error)?{??
  • ????????????NSLog(@"error:%@",?error);??
  • ???????????//?[error?release];??
  • ????????}??
  • ????}??
  • ?????//讀取??
  • ????NSDictionary?*dictionaryFromFile2?=?(NSDictionary?*)[NSPropertyListSerialization?propertyListWithData:[NSData?dataWithContentsOfFile:@"/Users/zhoumoban/Desktop/test2.plist"]?options:0?format:NULL?error:&error];??
  • ????NSLog(@"===%@",[dictionaryFromFile2?objectForKey:@"intNumber"]);??
  • 轉(zhuǎn)載于:https://www.cnblogs.com/lovewx/p/4158999.html

    與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的iOS 各种系统文件目录 临时,缓存,document,lib,归档,序列化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。