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.
- # Import "dataprocessAppDelegate. h"
- @ Implementation dataprocessAppDelegate
- @ Synthesize window;
- @ Synthesize dataArray;
- -(NSString *) pathFileForProcess :( NSString *) pathName {
- NSArray * directory = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
- Return [[directory objectAtIndex: 0] stringByAppendingPathComponent: pathName];
- }
- Z
- -(Void) writeDataToFile {
- FirstData = [[NSString alloc] initWithString: @ "im first! "];
- SecondData = [[NSString alloc] initWithString: @ "im secondData! "];
- ThirdData = [[NSString alloc] initWithString: @ "im thirdData! "];
- NSLog (@ "write: \ n first: % @ \ nscond: % @ \ nthird: % @", firstData, secondData, thirdData );
- NSMutableArray * tmp = [[NSMutableArray alloc] init];
- [Tmp addObject: firstData];
- [Tmp addObject: secondData];
- [Tmp addObject: thirdData];
- Self. dataArray = tmp;
- [Tmp release];
- [FirstData release]; [secondData release]; [thirdData release];
- BOOL bWrite = [dataArray writeToFile: [self pathFileForProcess: @ "myTest.txt"] atomically: YES];
- } // Read attributes
- -(Void) readDataFromFile {
- If ([[NSFileManager defaultManager] fileExistsAtPath: [selfpathFileForProcess: @ "myTest.txt"]) {
- NSMutableArray * tmpRead = [[NSMutableArray alloc] initWithContentsOfFile: [selfpathFileForProcess: @ "myTest.txt"];
- Self. dataArray = tmpRead;
- [TmpRead release];
- FirstData = [dataArray objectAtIndex: 0];
- SecondData = [dataArray objectAtIndex: 1];
- ThirdData = [dataArray objectAtIndex: 2];
- NSLog (@ "read: \ n first: % @ \ nscond: % @ \ nthird: % @", firstData, secondData, thirdData );
- Return;
- }
- NSLog (@ "process firle doesnt exits! ");
- }
- # Pragma mark ------- object -------------
- // Archive write
- -(Void) processObjectWrite {
- Person * pObject = [[person alloc] init];
- PObject. name = [[NSString alloc] initWithString: @ "wenQiang"];
- PObject. Permission sion = [[NSString alloc] initWithString: @ "project manager"];
- // [PObject setAge: 24 andMarry: NO];
- // NSMutableArray * testData = [[NSMutableArray alloc] init];
- NSMutableData * data = [NSMutableData alloc] init];
- NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
- [Archiver encodeObject: pObject forKey: @ "kObject"];
- [Archiver finishEncoding];
- Boolean bWrite = [data writeToFile: [self pathFileForProcess: @ "object2.txt"] atomically: YES];
- If (bWrite) NSLog (@ "OK..."); else NSLog (@ "write error! ");
- [Archiver release];
- // [PObject release];
- }. Archive read
- -(Void) processObjectRead {
- NSMutableData * data = [[NSMutableData alloc] initWithContentsOfFile: [selfpathFileForProcess: @ "object2.txt"];
- NSLog (@ "data % @ ..", data );
- NSKeyedUnarchiver * unchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
- Person * tmp = [unchiver decodeObjectForKey: @ "kObject"];
- [Unchiver finishDecoding];
- NSLog (@ "OBJECT: name: % @ partition sion: % @ \ nage: % @ \ n marry: % @", tmp. name, tmp. Partition sion );
- [Unchiver release];
- // [Tmp release];
- // Implementation
- -(Void) applicationDidFinishLaunching :( UIApplication *) application {
-
- // Override point for customization after application launch
- // [Self writeDataToFile];
- // [Self readDataFromFile];
- [Self processObjectWrite];
- [Self processObjectRead];
- [Window makeKeyAndVisible];
- }
- -(Void) dealloc {
- [Window release];
- [DataArray release];
- [Super dealloc];
- }
- @ End
- // The following are custom classes
- # Pragma mark --- class person --
- # Define kName @ "keyName"
- # Define k1_sion @ "key1_sion"
- # Define kAge @ "keyAge"
- # Define kMarry @ "keyMarry"
- @ Implementation person
- @ Synthesize name;
- @ Synthesize sequence Sion;
- # Pragma mark ---- nscoding delegate 2 method --
- -(Void) encodeWithCoder :( NSCoder *) ACO {
- [Ecoder encodeObject: name forKey: kName];
- [Ecoder encodeObject: Authorization sion forKey: kProfession];
- // [Ecoder encodeObject: Age forKey: kAge];
- // [Ecoder encodeObject: marry forKey: kMarry];
- }
- -(Id) initWithCoder :( NSCoder *) aDecoder {
- If (self = [super init]) {
- Self. name = [aDecoder decodeObjectForKey: kName];
- Self. Permission sion = [aDecoder decodeObjectForKey: kProfession];
- // Age = [aDecoder decodeObjectForKey: kAge];
- // Marry = [aDecoder decodeObjectForKey: kMarry];
- }
- Return self;
- }
- # Pragma mark --------------- NSCopying 1 method -------------
- -(Id) copyWithZone :( NSZone *) zone {
- Person * tmp = [[self class] allocWithZone: zone] init];
- Tmp. name = [self. name copy];
- Tmp. Permission sion = [self. Permission sion copy];
- Return nil;
- }
- -(Void) dealloc {
- [Name release];
- [Permission sion release];
- [Super dealloc];
- }
- //-(Void) setAge :( NSInteger) age andMarry :( Boolean) B {
- // Age = age;
- // Marry = B;
- //}
- @ End
Summary:IPhone Data StorageAttributes and archivesArchiveI hope this article will help you!