自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone數(shù)據(jù)存儲之屬性和歸檔Archive

移動開發(fā) iOS
iPhone每個(gè)應(yīng)用都會有三個(gè)文件夾分別是documents、tmp、library分別稱為存儲應(yīng)用的數(shù)據(jù),臨時(shí)數(shù)據(jù),數(shù)據(jù)庫。我們要保存的數(shù)據(jù)會在documents中。

iPhone數(shù)據(jù)存儲之屬性和歸檔Archive是本文要介紹的內(nèi)容,在iPhone中有四種方式可以永久存儲數(shù)據(jù)分別是屬性列表、歸檔、SQLITE3、coredata。

先來解釋一下,前兩者、后二者操作的時(shí)候有些地方是相同的,以屬性列表和歸檔來說都會用writeToFile/URL:path atomically:flag 和initWithContentofFile/URL:path;兩都都不能直接操作基本數(shù)據(jù)類型,不過前者不能操作自定義的類,而后者可以通過實(shí)現(xiàn)NSCoding協(xié)議來達(dá)到目的。另外要說點(diǎn)的就是IPHONE每個(gè)應(yīng)用都會有三個(gè)文件夾分別是documents、tmp、library分別稱為存儲應(yīng)用的數(shù)據(jù),臨時(shí)數(shù)據(jù)數(shù)據(jù)庫。我們要保存的數(shù)據(jù)會在documents中。由于時(shí)間關(guān)系抽空再把這個(gè)文字寫完整些。

  1. #import "dataprocessAppDelegate.h"  
  2. @implementation dataprocessAppDelegate  
  3. @synthesize window;  
  4. @synthesize dataArray;  
  5. -(NSString*)pathFileForProcess:(NSString *)pathName{  
  6. NSArray *directory=NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES);  
  7. return [[directory objectAtIndex:0] stringByAppendingPathComponent:pathName];  
  8. }  
  9. z  
  10. -(void)writeDataToFile{  
  11. firstData = [[NSString alloc] initWithString:@"im first!"];  
  12. secondData = [[NSString alloc] initWithString:@"im secondData!"];  
  13. thirdData = [[NSString alloc] initWithString:@"im thirdData!"];  
  14. NSLog(@"write:\n first: %@ \nscond: %@ \nthird: %@", firstData, secondData, thirdData);  
  15. NSMutableArray *tmp = [[NSMutableArray alloc] init];  
  16. [tmp addObject:firstData];  
  17. [tmp addObject:secondData];  
  18. [tmp addObject:thirdData];  
  19. self.dataArray = tmp;  
  20. [tmp release];  
  21. [firstData release];[secondData release];[thirdData release];  
  22. BOOL bWrite = [dataArray writeToFile:[self pathFileForProcess:@"myTest.txt"]atomically:YES];  
  23. }//屬性讀  
  24. -(void)readDataFromFile{  
  25. if([[NSFileManager defaultManager] fileExistsAtPath:[selfpathFileForProcess:@"myTest.txt"]]){  
  26.       NSMutableArray  *tmpRead = [[NSMutableArray alloc] initWithContentsOfFile:[selfpathFileForProcess:@"myTest.txt"]];  
  27.   self.dataArray = tmpRead;  
  28.       [tmpRead release];  
  29.     firstData = [dataArray objectAtIndex:0];  
  30.     secondData = [dataArray objectAtIndex:1];  
  31.     thirdData = [dataArray objectAtIndex:2];  
  32.     NSLog(@"read:\n first: %@ \nscond: %@ \nthird: %@", firstData, secondData, thirdData);  
  33. return;  
  34. }  
  35. NSLog(@"PROCESS FIRLE DOESNT EXITS!");  
  36. }  
  37. #pragma mark -------object-------------  
  38. //歸檔寫  
  39. -(void)processObjectWrite{  
  40. person *pObject= [[person alloc] init];  
  41. pObject.name = [[NSString alloc] initWithString:@"wenQiang"];  
  42. pObject.profession = [[NSString alloc] initWithString:@"project manager"];  
  43. //[pObject setAge:24 andMarry: NO];  
  44. //NSMutableArray *testData = [[NSMutableArray alloc] init];  
  45. NSMutableData *data=[[NSMutableData alloc] init];  
  46. NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
  47. [archiver encodeObject:pObject forKey:@"kObject"];  
  48. [archiver finishEncoding];  
  49. Boolean bWrite = [data writeToFile:[self pathFileForProcess:@"object2.txt"]atomically:YES];  
  50. if(bWrite) NSLog(@"ok..."); else NSLog(@"write error!");  
  51. [archiver release];  
  52. //[pObject release];  
  53. }、、歸檔讀  
  54. -(void)processObjectRead{  
  55. NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:[selfpathFileForProcess:@"object2.txt"]];  
  56. NSLog(@"data %@..", data);  
  57. NSKeyedUnarchiver *unchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  58. person *tmp = [unchiver decodeObjectForKey:@"kObject"];  
  59. [unchiver finishDecoding];  
  60. NSLog(@"OBJECT: name: %@ profession: %@\nage: %@\n marry:%@", tmp.name, tmp.profession);  
  61. [unchiver release];  
  62. //[tmp release];  
  63. //實(shí)現(xiàn)  
  64. - (void)applicationDidFinishLaunching:(UIApplication *)application {      
  65.  
  66.     // Override point for customization after application launch  
  67. //[self writeDataToFile];  
  68. //[self readDataFromFile];  
  69. [self processObjectWrite];  
  70. [self processObjectRead];  
  71.     [window makeKeyAndVisible];  
  72. }  
  73. - (void)dealloc {  
  74.     [window release];  
  75. [dataArray release];  
  76.     [super dealloc];  
  77. }  
  78. @end  
  79. //以下是自定義的類  
  80. #pragma mark---class person--  
  81. #define       kName              @"keyName"  
  82. #define       kProfession @"keyProfession"  
  83. #define       kAge @"keyAge"  
  84. #define       kMarry @"keyMarry"  
  85. @implementation person  
  86. @synthesize name;  
  87. @synthesize profession;  
  88. #pragma mark----nscoding delegate 2 method--  
  89. - (void)encodeWithCoder:(NSCoder *)aCoder{  
  90. [aCoder encodeObject:name forKey: kName];  
  91. [aCoder encodeObject:profession forKey: kProfession];  
  92. // [aCoder encodeObject:Age forKey: kAge];  
  93. // [aCoder encodeObject:marry forKey:kMarry];  
  94. }  
  95. - (id)initWithCoder:(NSCoder *)aDecoder{  
  96. if(self = [super init]){  
  97. self.name = [aDecoder decodeObjectForKey:kName];  
  98. self.profession = [aDecoder decodeObjectForKey:kProfession];  
  99. //Age = [aDecoder decodeObjectForKey:kAge];  
  100. // marry=[aDecoder decodeObjectForKey:kMarry];  
  101. }  
  102. return self;  
  103. }  
  104. #pragma mark ---------------NSCopying 1 method-------------  
  105. - (id)copyWithZone:(NSZone *)zone{  
  106. person *tmp = [[[self class] allocWithZone:zone] init];  
  107. tmp.name = [self.name copy];  
  108. tmp.profession = [self.profession copy];  
  109. return nil;  
  110. }  
  111. -(void)dealloc{  
  112. [name release];  
  113. [profession release];  
  114. [super dealloc];  
  115. }  
  116. //-(void)setAge:(NSInteger)age andMarry:(Boolean)b{  
  117. // Age = age;  
  118. // marry = b;  
  119. //}  
  120. @end 

小結(jié):iPhone數(shù)據(jù)存儲之屬性和歸檔Archive的內(nèi)容介紹完了,希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 新浪博客
相關(guān)推薦

2011-08-11 17:15:54

iPhone歸檔

2013-05-03 11:01:22

iOS開發(fā)歸檔永久存儲數(shù)據(jù)

2011-08-16 18:42:42

iPhone開發(fā)Release

2018-11-22 10:40:40

存儲備份數(shù)據(jù)

2020-09-17 13:15:20

騰訊云冷數(shù)據(jù)存儲

2010-04-02 15:25:40

云歸檔

2009-01-19 16:09:44

NetApp賽門鐵克歸檔

2010-04-02 15:20:18

云存儲

2011-07-27 12:52:39

賽門鐵克數(shù)據(jù)歸檔備份廠商

2011-05-31 17:32:32

Android SharedPref

2011-08-17 15:19:38

iPhone應(yīng)用數(shù)據(jù)

2011-06-07 17:16:47

iPhone 數(shù)據(jù)

2017-11-07 08:36:58

云計(jì)算歸檔存儲

2019-11-26 15:12:08

數(shù)據(jù)存儲B+樹

2011-03-08 09:58:21

海量數(shù)據(jù)

2015-07-09 13:47:37

IOSFMDB

2011-06-28 12:07:00

郵件歸檔郵件服務(wù)器SaaS

2018-03-20 09:36:57

數(shù)據(jù)倉庫數(shù)據(jù)存儲知識

2021-02-22 15:03:01

金山云歸檔存儲數(shù)據(jù)

2011-06-28 11:30:21

郵件歸檔
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號