Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON/XML解析iphone NSPredicate_Iphone_Xml_Json_Parsing_Nspredicate - Fatal编程技术网

JSON/XML解析iphone NSPredicate

JSON/XML解析iphone NSPredicate,iphone,xml,json,parsing,nspredicate,Iphone,Xml,Json,Parsing,Nspredicate,我正在尝试创建一个使用xml或json来显示一些内容的小应用程序 我有很多“视图”,我只想从她的id中选择一个 XML是这样的(如果您愿意,我可以放json,但它很难读懂): 帮助 助手 德夫麦克 开发iPhone 例子 帮助 德夫麦克 巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴 第1课 唐纳德 德夫麦克 第1课 巴巴拉巴巴拉巴巴拉 A.UIKit UIKit是。。。。。 我有一个数组,其中包含3个NSDict

我正在尝试创建一个使用xml或json来显示一些内容的小应用程序

我有很多“视图”,我只想从她的id中选择一个

XML是这样的(如果您愿意,我可以放json,但它很难读懂):


帮助
助手
德夫麦克
开发iPhone
例子
帮助
德夫麦克
巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴巴
第1课
唐纳德
德夫麦克
第1课
巴巴拉巴巴拉巴巴拉
A.UIKit
UIKit是。。。。。
我有一个数组,其中包含3个NSDictionary(3视图)

我如何通过她的id只选择一个视图

提前感谢


BiB1

不能直接在XML上使用
NSPredicate
。您必须将XML(或JSON)解析成一些标准的Cocoa容器(
NSArray
,等等),然后在上面使用它


能够直接过滤XML的唯一方法是使用XPath之类的东西,尽管这样做需要转到libxml。

只要数据变量实际上是一个数组,其中包含一个键为“id”的字典,它就应该可以工作

NSArray*数据=
NSString*currentViewId=@“主页”;
NSArray*filterd=[data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@“(id=%@)”,currentViewId]];

尝试将xml重新格式化为plist(您可以使用Xcode plist编辑器或自己滚动),使用:

在.m文件中:

   NSString *Views_DataPath = [[NSBundle mainBundle] pathForResource:@"nameOfYourPlist" ofType:@"plist"];   
    Views_List = [[NSArray alloc] initWithContentsOfFile:Views_DataPath];
然后访问每个dict:

[[Views_List objectAtIndex: 0] valueForKey:@"Title"];

在本例中(0)将返回“Home”

是的,我知道,我不想直接在XML上使用NSPredicate。我的XML数据通过XMLReader转换为NSDictionary。但我找不到如何使用NSPredicate从NSDictionary(或NSArray)中的is id中选择“父项”,不幸的是,如果视图以不同的顺序发送,我无法使用objectAtIndex
<plist version="1.0">
 <array>
    <dict>
        <key>Title</key><string>Home</string>
        <key>Image</key><string>Home.png</string>
    </dict>
    <dict>
        <key>Title</key><string>Car</string>
        <key>Image</key><string>Car.png</string>
    </dict>
</array>
</plist
NSArray *Views_List;
   NSString *Views_DataPath = [[NSBundle mainBundle] pathForResource:@"nameOfYourPlist" ofType:@"plist"];   
    Views_List = [[NSArray alloc] initWithContentsOfFile:Views_DataPath];
[[Views_List objectAtIndex: 0] valueForKey:@"Title"];