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
    
    
  • 相关阅读:
    韦德螺旋: 这真是一个螺旋吗?
    山上你能看到什么动物?
    你能够30秒内一字不差的念完它吗? 注意, 是读“颜色”, 不是让你识字.
    路透斯沃德的不可能的三角形
    换个角度, 青蛙也许就是白马王子
    这是一张很有趣的图片, 通常女性会先看到月亮, 男性会先看到人脸. 如果相反, 表示你体内的异性荷尔蒙偏高哦!
    亲吻的情侣幻觉: 这幅虚幻的亲吻由美国艺术家杰里•唐恩创作.
    PostgreSQL的 initdb 源代码分析之七
    PostgreSQL的initdb 源代码分析之六
    PostgreSQL的initdb 源代码分析之五
  • 原文地址:https://www.cnblogs.com/lm3515/p/1902119.html
Copyright © 2011-2022 走看看