Attributes and Archive of iPhone Data Storage

Source: Internet
Author: User

IPhone Data StorageAttributes and archivesArchiveIs the content to be introduced in this article, there are four ways to permanent iPhoneStore DataAttribute list, archive, SQLITE3, and coredata.

The first and second operations are the same in some places. For attribute lists and archives, writeToFile/URL: path atomically: flag and initWithContentofFile/URL are used: path; neither of them can directly operate on the basicDataType, but the former cannot operate the custom class, while the latter can achieve the goal through implementing the NSCoding protocol. In addition, the point isIPHONEEach application has three folders: documents, tmp, and library.StorageApplicationData, TemporaryData,DataLibrary. TheDataWill be in documents. Due to the time relationship, write the text more completely.

 
 
  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. } // Read attributes
  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. // Archive write
  39. -(Void) processObjectWrite {
  40. Person * pObject = [[person alloc] init];
  41. PObject. name = [[NSString alloc] initWithString: @ "wenQiang"];
  42. PObject. Permission sion = [[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. }. Archive read
  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: % @ partition sion: % @ \ nage: % @ \ n marry: % @", tmp. name, tmp. Partition sion );
  61. [Unchiver release];
  62. // [Tmp release];
  63. // Implementation
  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. // The following are custom classes
  80. # Pragma mark --- class person --
  81. # Define kName @ "keyName"
  82. # Define k1_sion @ "key1_sion"
  83. # Define kAge @ "keyAge"
  84. # Define kMarry @ "keyMarry"
  85. @ Implementation person
  86. @ Synthesize name;
  87. @ Synthesize sequence Sion;
  88. # Pragma mark ---- nscoding delegate 2 method --
  89. -(Void) encodeWithCoder :( NSCoder *) ACO {
  90. [Ecoder encodeObject: name forKey: kName];
  91. [Ecoder encodeObject: Authorization sion forKey: kProfession];
  92. // [Ecoder encodeObject: Age forKey: kAge];
  93. // [Ecoder encodeObject: marry forKey: kMarry];
  94. }
  95. -(Id) initWithCoder :( NSCoder *) aDecoder {
  96. If (self = [super init]) {
  97. Self. name = [aDecoder decodeObjectForKey: kName];
  98. Self. Permission sion = [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. Permission sion = [self. Permission sion copy];
  109. Return nil;
  110. }
  111. -(Void) dealloc {
  112. [Name release];
  113. [Permission sion release];
  114. [Super dealloc];
  115. }
  116. //-(Void) setAge :( NSInteger) age andMarry :( Boolean) B {
  117. // Age = age;
  118. // Marry = B;
  119. //}
  120. @ End

Summary:IPhone Data StorageAttributes and archivesArchiveI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.