归档和解档

对于系统定义不可变的类型的对象,复制时不会产生新的对象【ns numb string   array dictionary】





r只写

r+xie读

w只读

w+清理完在写

a追加

a+





数据持久化

NSUserDefaults 只能存系统定义的类型,不能存太多或是大数据,可用于系统偏好设置;

plist不能存自定义的数据

NSKeyedArchiver

SQLite数据库

Core Data

XML、JSON简单易于传输



//c语言的文件操作

        FILE *fp = fopen("/Users/apple/Desktop/test.txt", "r+");

        if (fp == NULL) {

            return -1;

        }

        

        char a[10] = {0};

        size_t res = fread(a, 1, 2, fp);

        if (res == 0) {

            printf("read failed\n");

        }

        printf("%s\n", a);

        fpos_t pos;

        fgetpos(fp, &pos);

        printf("pos: %lld\n", pos);

        

        size_t result = fwrite("xyzwopq", 1, 6, fp);

        fgetpos(fp, &pos);

        printf("pos: %lld\n", pos);

        

        if (result == 0) {

            printf("write failed\n");

        }

        else {

            printf("write success\n");

        }

        

        res = fread(a, 1, 2, fp);

        if (res == 0) {

            printf("read failed\n");

        }

        printf("%s\n", a);

        fgetpos(fp, &pos);

        printf("pos: %lld\n", pos);

        

        fclose(fp);





  //属性列表的方式,只能存系统定义的类型

    NSArray *array1 = [NSArray arrayWithContentsOfFile:@"/Users/apple/Desktop/test.plist"];

    NSLog(@"%@", array1);

    

    //1. 只能存系统定义的类型

    //2. 不能存太多数据或者是大数据

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];//单(实)例类

    [userDefault setInteger:2340 forKey:@"age"];

//

    NSInteger age = [userDefault integerForKey:@"age"];

    NSLog(@"%d", age);

    

      //二进制编码

    NSString *str = @"123abc";

    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"%@", data);

    

    //从文件中读取数据

    data = [NSData dataWithContentsOfFile:@"/Users/apple/Desktop/a.out"];

    NSLog(@"%@", data);

    

    NSMutableData *mData = [NSMutableData data];

    [mData appendData:data];

    [mData appendData:data];

    

//项文件中写数据

    [mData writeToFile:@"/Users/apple/Desktop/b.out" atomically:YES];

    

    NSURL *url = [NSURL URLWithString:@"http://sqlite.org/images/foreignlogos/nokia.gif"];

    NSData *nokia = [NSData dataWithContentsOfURL:url];

    [nokia writeToFile:@"/Users/apple/Desktop/nokia.gif" atomically:YES];



//////不是系统的数据需要编码取归档,译码再解档//////

@implementation Dog

要实现NSCoding两个方法

- (void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:_name forKey:kDogNameKey];

}



- (id)initWithCoder:(NSCoder *)aDecoder

{

    self = [super init];

    if (self) {

        self.name = [aDecoder decodeObjectForKey:kDogNameKey];

    }

    

    return self;

}

@end



、、、、、、、、、、、、、、、、、、、、、、



/////////归档与解档



 NSString *str = @"abc";

    [NSKeyedArchiver archiveRootObject:str toFile:@"/Users/apple/Desktop/test.plist"];

    

    NSString *str1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/test.plist"];

    NSLog(@"%@", str1);

    

    Student *stu = [[Student alloc] init];

    stu.name = @"Zhangsan";

    stu.age = 30;

    

    [NSKeyedArchiver archiveRootObject:stu toFile:@"/Users/apple/Desktop/student.plist"];

    

    Student *stu1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/student.plist"];

    NSLog(@"%p, %p", stu, stu1);

    NSLog(@"%@:%d, %@:%d", stu.name, stu.age, stu1.name, stu1.age);

    

    NSData *stuData = [NSKeyedArchiver archivedDataWithRootObject:stu];

    [stuData writeToFile:@"/Users/apple/Desktop/stu.plist" atomically:YES];

    

    NSData *stuData2 = [NSData dataWithContentsOfFile:@"/Users/apple/Desktop/stu.plist"];

    Student *stu3 = [NSKeyedUnarchiver unarchiveObjectWithData:stuData2];

    NSLog(@"%@:%d---%@:%d", stu.name, stu.age, stu3.name, stu3.age);

    

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

    [userDefault setObject:stuData forKey:@"xxxx"];

    NSData *stuData3 = [userDefault objectForKey:@"xxxx"];

    Student *stu4 = [NSKeyedUnarchiver unarchiveObjectWithData:stuData3];

    NSLog(@"%@:%d", stu4.name, stu4.age);

    

    stu4.dog = [[Dog alloc] init];

    stu4.dog.name = @"小强";

    [NSKeyedArchiver archiveRootObject:stu4 toFile:@"/Users/apple/Desktop/dog.plist”];



、、、、、、、、、、、、、、、、、、、、、、、、、、

iOS沙盒中一共有四个文件夹 • Documents/tmp/app/Library



• 系统提供了获取这些目录的方法



•  NSHomeDirectory()



•  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirecto ry, NSUserDomainMask, YES);



•  NSString *docDirectory = [paths objectAtIndex: 0];



•  NSTemporaryDirectory()



• 获取当前程序中资源文件路径



• NSString *imagePath = [[NSBundle mainBundle] pathForResource:



@”image” ofType: @”png”]; 







、、、、、、、沙盒机制、、、、、、、、



    NSString *home = NSHomeDirectory();

    NSLog(@"%@", home);

    

    NSString *doc = [home stringByAppendingPathComponent:@"Documents"];

    NSLog(@"%@", doc);

    

    //2.

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

    NSLog(@"%@", array);

    NSString *doc2 = [array firstObject];

    NSLog(@"%@", doc2);

    

    //3. tmp

    NSString *tmp = NSTemporaryDirectory();

    NSLog(@"%@", tmp);

    

    NSString *name = @"戴维营教育";

    NSString *path = [doc stringByAppendingPathComponent:@"test.plist"];

    [name writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    //4.

    NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"testDB" ofType:@"db"];



    NSLog(@"%@", dbPath);

 

你可能感兴趣的:(归档和解档)