zoukankan      html  css  js  c++  java
  • iphone 数据存储之属性和归档archive

         在IPHONE中有四种方式可以永久存储数据分别是属性列表、归档、SQLITE3、coredata。前两者、后二者操作的时候有些地方是相同的,以属 性列表和归档来说都会用writeToFile/URL:path atomically:flag 和initWithContentofFile/URL:path;两都都不能直接操作基本数据类型,不过前者不能操作自定义的类,而后者可以通过实现 NSCoding协议来达到目的。另外要说点的就是IPHONE每个应用都会有三个文件夹分别是documents、tmp、library分别称为存储 应用的数据,临时数据,数据库。我们要保存的数据会在documents中。由于时间关系抽空再把这个文字写完整些。

    #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:[self pathFileForProcess:@"myTest.txt"]]){
          NSMutableArray  *tmpRead = [[NSMutableArray alloc] initWithContentsOfFile:[self pathFileForProcess:@"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:[self pathFileForProcess:@"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];
    
    //实现
    - (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
    
    
  • 相关阅读:
    .Net转Java自学之路—Spring框架篇二(IOC注解、AOP)
    .Net转Java自学之路—Spring框架篇一(IOC入门)
    深入浅出ES6(七):箭头函数 Arrow Functions
    深入浅出ES6(六):解构 Destructuring
    深入浅出ES6(五):不定参数和默认参数
    深入浅出ES6(四):模板字符串
    深入浅出ES6(三):生成器 Generators
    深入浅出ES6(二):迭代器和for-of循环
    深入浅出ES6(一):ES6是什么
    父子页面之间跨域通信的方法
  • 原文地址:https://www.cnblogs.com/lm3515/p/1902119.html
Copyright © 2011-2022 走看看