iPhone:使用简单的键/值字符串创建二进制 Plist 的最快方法

发布于 2024-08-31 14:26:27 字数 674 浏览 6 评论 0原文

在 iPhone 上使用基于简单字符串的键/值对创建二进制 plist 的最佳方法是什么?我需要创建一个包含食谱和成分列表的 plist。然后我希望能够将其读入 NSDictionary,这样我就可以执行类似

NSString *ingredients = [recipes objectForKey:@"apple pie"];

通过 HTTP 请求读取 XML 数据文件并希望将所有键值对解析到 plist 中的操作。 XML 可能看起来像:

<recipes>
   <recipe> 
     <name>apple pie</name>
     <ingredients>apples and pie</ingredients>
   </recipe>
   <recipe>
     <name>cereal</name>
     <ingredients>milk and some other ingredients</ingredients>
   </recipe>
</recipes>

理想情况下,我能够在运行时将其写入 plist,然后能够在运行时读取它并将其转换为 NSDictionary。

What's the best way to create a binary plist on the iPhone with simple string based key/value pairs? I need to create a plist with a list of recipe and ingredients. I then want to be able to read this into an NSDictionary so I can do something like

NSString *ingredients = [recipes objectForKey:@"apple pie"];

I'm reading in an XML data file through an HTTP request and want to parse all of the key value pairs into the plist. The XML might look something like:

<recipes>
   <recipe> 
     <name>apple pie</name>
     <ingredients>apples and pie</ingredients>
   </recipe>
   <recipe>
     <name>cereal</name>
     <ingredients>milk and some other ingredients</ingredients>
   </recipe>
</recipes>

Ideally, I'll be able to write this to a plist at runtime, and then be able to read it and turn it into an NSDictionary later at runtime as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-09-07 14:26:27

在 Objective-C 中创建属性列表

这包含使用 CoreFoundation 或 Cocoa 创建属性列表文件的信息。

Creating a Property List in Objective-C

This contains information for creating a property list file using either CoreFoundation or Cocoa.

行至春深 2024-09-07 14:26:27

对于您的具体情况,请查看 NSDictionary 的接口并使用:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
+ (id)dictionaryWithContentsOfFile:(NSString *)path;

如果您只需要一本相当小的字典,您也可以尝试 NSUserDefaults。

- (void)setObject:(id)value forKey:(NSString *)defaultName;
- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;

For your specific case, look at the interface for NSDictionary and use:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
+ (id)dictionaryWithContentsOfFile:(NSString *)path;

If you only need one fairly small dictionary, you could also try NSUserDefaults.

- (void)setObject:(id)value forKey:(NSString *)defaultName;
- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文