iOS之NSKeyedArchiver的序列化存储

来源:互联网 发布:nginx ab压力测试工具 编辑:程序博客网 时间:2024/05/19 07:09
        NSString *rootPath = NSHomeDirectory();        NSString *path = [rootPath stringByAppendingPathComponent:@"test.txt"];        NSLog(@"%@",path);                NSMutableData *data = [NSMutableData data];        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];        NSArray *array = @[@"one",@"two"];        [archiver encodeObject:array forKey:@"array"];        [archiver finishEncoding];        [data writeToFile:path atomically:YES];        if ([data writeToFile:path atomically:YES]) {            NSLog(@"success");        }else{            NSLog(@"fail");        }                /*         *  取出数据         */        NSData *datas = [NSData dataWithContentsOfFile:path];        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:datas];        NSArray *arrays = [unarchiver decodeObjectForKey:@"array"];        NSLog(@"%@",arrays);        

0 0
原创粉丝点击