iPhone數(shù)據(jù)存儲之屬性和歸檔Archive
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è)文字寫完整些。
- #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];
- }//屬性讀
- -(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-------------
- //歸檔寫
- -(void)processObjectWrite{
- person *pObject= [[person alloc] init];
- pObject.name = [[NSString alloc] initWithString:@"wenQiang"];
- pObject.profession = [[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];
- }、、歸檔讀
- -(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: %@ profession: %@\nage: %@\n marry:%@", tmp.name, tmp.profession);
- [unchiver release];
- //[tmp release];
- //實(shí)現(xiàn)
- - (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
- //以下是自定義的類
- #pragma mark---class person--
- #define kName @"keyName"
- #define kProfession @"keyProfession"
- #define kAge @"keyAge"
- #define kMarry @"keyMarry"
- @implementation person
- @synthesize name;
- @synthesize profession;
- #pragma mark----nscoding delegate 2 method--
- - (void)encodeWithCoder:(NSCoder *)aCoder{
- [aCoder encodeObject:name forKey: kName];
- [aCoder encodeObject:profession forKey: kProfession];
- // [aCoder encodeObject:Age forKey: kAge];
- // [aCoder encodeObject:marry forKey:kMarry];
- }
- - (id)initWithCoder:(NSCoder *)aDecoder{
- if(self = [super init]){
- self.name = [aDecoder decodeObjectForKey:kName];
- self.profession = [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.profession = [self.profession copy];
- return nil;
- }
- -(void)dealloc{
- [name release];
- [profession release];
- [super dealloc];
- }
- //-(void)setAge:(NSInteger)age andMarry:(Boolean)b{
- // Age = age;
- // marry = b;
- //}
- @end
小結(jié):iPhone數(shù)據(jù)存儲之屬性和歸檔Archive的內(nèi)容介紹完了,希望本文對你有所幫助!