Plist(写入沙盒)

  NSString *username = @"name";

    NSString *password = @"password";

    

    //1.plist --> 文件要放到沙盒里

    //沙盒:每个应用程序对应于沙盒中的一个特定文件夹。在访问沙盒中的文件时,每个应用程序只能访问自己对应的文件夹

    //Documents:用于存放需要永久性存储的文件

    //tmp:用于存放临时性的文件

   NSString *path = NSHomeDirectory();

//    NSLog(@"path = %@",path);

    path = [path stringByAppendingPathComponent:@"/Documents/1.plist"];

    

    NSArray *plistArr = @[username,password];

    //atomically:YES --> 文件会被先放到缓冲区,等所有文件就位后,一次性写入

    //           NO --> 立即写入

    [plistArr writeToFile:path atomically:YES];

    

    //2.归档

    NSString *path2 = NSHomeDirectory();

    path2 = [path2 stringByAppendingPathComponent:@"Documents/2.archive"];

    NSLog(@"%@",path2);

    [NSKeyedArchiver archiveRootObject:plistArr toFile:path2];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
批量导出plist文件通常是在编程中,特别是使用Objective-C或Swift进行iOS和macOS应用程序开发时,处理大量数据的一种常见操作。Plist(Property List)是Apple平台用于存储可序列化的数据结构,如键值对、数组和字典。 批量导出plist的过程通常包括以下几个步骤: 1. **数据准备**:首先,你需要有一个包含要导出的数据集合,这可能是程序中的一个结构体数组或者字典。 2. **创建NSKeyedArchiver实例**:在Objective-C中,你可以使用`NSKeyedArchiver`类,而在Swift中则用`PropertyListEncoder`。这是用来将数据编码成plist格式的工具。 ```swift let encoder = PropertyListEncoder() ``` 3. **开始编码**:调用`encode(to:writingOptions:error:)`方法,指定要保存的路径和编码选项。 ```swift do { try encoder.encode(yourData, to: URL(fileURLWithPath: "output.plist"), options: .prettyPrinted) } catch let error as NSError { // 处理错误 } ``` 4. **Objective-C示例**: ```objective-c NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithFormat:NSPropertyListXMLFormat_v1_0]; [archiver encodeObject:yourData forKey:@"YourDictionaryKey"]; [archiver finishEncoding]; [archiver writeToFile:@"output.plist" atomically:YES]; ``` 5. **写入文件**:上述代码会将数据写入到指定的plist文件中。`prettyPrinted`选项(Objective-C中的`NSPropertyListXMLFormat_v1_0`)会让输出的plist文件格式更易读。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值