关于NSKeyedArchiver的编码格式

时间:2021-07-08 05:25:27

NSKeyedArchiver在linux的实现默认的格式是二进制:

NSArray *ary = @[@"hello",@"world",@"!!!",@11];

[NSKeyedArchiver archiveRootObject:ary toFile:@"./foo.dat"];

我们还可以选择XML格式:

NSMutableData *data = [NSMutableData data];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
            initForWritingWithMutableData:data];

//The available formats are NSPropertyListXMLFormat_v1_0 and NSPropertyListBinaryFormat_v1_0

        archiver.outputFormat = NSPropertyListXMLFormat_v1_0;
        [archiver encodeObject:ary forKey:@"root"];
        [archiver finishEncoding];
        [data writeToFile:@"./foo.dat" atomically:YES];