简体   繁体   English

如何从plist读取到NSString

[英]How to read from plist to an NSString

i have a pList with this format 我有一个这种格式的pList

<array>
    <dict>
        <key>category</key>
        <string>desert</string>
        <key>numberOfPerson</key>
        <string>3</string>
        <key>recipeImage</key>
        <string>sutlac.jpg</string>
        <key>time</key>
        <string>15 dk</string>
        <key>recipeName</key>
        <string>Puding</string>
        <key>recipeDetail</key>

What i need to do is this; 我需要做的是这个; read the recipeIngredients from plist and write it to NSString. 从plist读取recipeIngredients并将其写入NSString。

How can i do this? 我怎样才能做到这一点?

Just load your plist into an NSArray get the dictionary out of it and read the contents from it: 只需将你的plist加载到NSArray从中获取字典并从中读取内容:

NSArray *recipes = [NSArray arrayWithContentsOfFile:plistPath];
NSDictionary *recipe = recipes[0];
NSString *cat = recipe[@"category"];
NSString *recipeName = recipe[@"recipeName"];

Your plist has an array as the outer structure. 你的plist有一个阵列作为外部结构。

You can use the array class method arrayWithContentsOfFile to load the property list into an array. 您可以使用数组类方法arrayWithContentsOfFile将属性列表加载到数组中。

Then you could use the array's description method, which will return a string display of the contents. 然后你可以使用数组的描述方法,它将返回内容的字符串显示。

The display method is intended for debugging, so the string you get back isn't intended to use programmatically. display方法用于调试,因此您获取的字符串不能以编程方式使用。

The code might look like this: 代码可能如下所示:

NSArray *array = [NSArray arrayWithContentsOfFile: path];
NSString *arrayString = [array description];
NSLog(@"Array string = %@", arrayString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM