gpt4 book ai didi

objective-c - Cocoa/Objective-C 将嵌套字符串/数据写入文件,如何?

转载 作者:行者123 更新时间:2023-12-03 17:29:39 25 4
gpt4 key购买 nike

我需要创建一个包含6条记录的xml .plist,其中记录5是用户输入的字符串,其余都是固定数据。我不确定这样做的最佳方法是什么?看起来应该很简单,但我还没有取得多大成功。

需要写入的数据如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AREA51</key>
<dict>
<key>ORIGIN</key>
<data>M45</data>

...(4 more records)...

(USER STRING)

...(1 more record)...

</dict>
</dict>
</plist>

我尝试过遵循一些示例,但我认为它们已经过时且不符合规范。谢谢!

最佳答案

我不知道这个教程怎么可能已经过时了,你的操作方式在十年内都没有改变。创建字典,并使用 -writeToFile:atomically: 方法。

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
// object first...
[NSDictionary dictionaryWithObjectsAndKeys:
@"M45", @"ORIGIN", // object first, then key
... // repeat
nil],
// then key...
@"AREA51",
nil];
[dict writeToFile:path atomically:NO];

(我不知道你是否真的需要原子写入。如果你不确定,将其更改为 YES 是安全的。)

关于objective-c - Cocoa/Objective-C 将嵌套字符串/数据写入文件,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com