【问题标题】:Read data from plist under a specific condition在特定条件下从 plist 中读取数据
【发布时间】:2014-03-27 18:22:31
【问题描述】:

我的 Xcode 项目中有一个像这样的 .plist

    <?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>Steve</key>
    <dict>
        <key>married</key>
        <true/>
        <key>website</key>
        <string>http://www.abc.com</string>
    </dict>
    <key>Bill</key>
    <dict>
        <key>website</key>
        <string>http://www.cab.com</string>
        <key>married</key>
        <false/>
    </dict>
</dict>
</plist>

所以现在我只想阅读该网站,如果已婚是真的。上面的 plist 只是我知道它没有意义的一个例子。你能帮我么 ? :) 编辑我现在这样做:

if ([[dict objectForKey:@"married"] boolValue]) {
        NSLog(@"(Y)");
    }

但是,如果我对每个 dict 都这样做,那就太费力了,我想我可以更轻松地做到这一点,但我不知道该怎么做。

【问题讨论】:

    标签: ios objective-c plist


    【解决方案1】:

    我没有清楚地理解你的问题,但根据我的理解,我认为你希望为每个循环实现一个,所以这里是你如何一次浏览整个 plist:

    NSMutableArray *arryaOfBools =  [[NSMutableArray alloc] init];//To keep the values if you wnat
    NSDictionary *dictioanry =  [[NSDictionary alloc] init];// initialize your plist
    // this for each will traverse through every dictionary present in the plist
    for(NSDictionary * dict in dictioanry ){
     /// you can do anything here as per your requirements 
     [arryaOfBools addObject:[dict valueForKey:@"married"]];
    }
    

    希望这能回答你的问题..

    编辑:

    这是您的代码的解决方案

    NSDictionary *senderDictionary = [NSDictionary dictionaryWithContentsOfFile:filePathforPlsit];
    
    for(NSString *keyValue in senderDictionary){
        [[senderDictionary valueForKey:keyValue] valueForKey:@"shortcut"];
    }
    

    【讨论】:

    • 也许我很傻(我想我是 xcode 的新手),但这对我不起作用。 “字典”和“arrayofBools”还是空的?你是对的,我想扫描整个 plist,但在字典中只有“married = true”的条目应该返回。
    • 你能把整个代码贴在你试图实现这一点的地方吗?或许能帮到你!
    • NSDictionary *senderDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath]; NSLog(@"%@",senderDictionary); //NSDictionary *antenne = senderDictionary[@"Antenne Bayern"]; //NSLog(@"%@",antenne); NSMutableArray *arrayofBools = [[NSMutableArray alloc] init]; NSDictionary *dictionary = [[NSDictionary alloc] init]; for(senderDictionary in dictionary) { [arrayofBools addObject:[senderDictionary objectForKey:@"shortcut"]]; }
    • 在这里。请帮帮我
    • 嘿,谢谢,但我现在在日志中没有得到它,它会打印:2014-03-27 22:37:56.710 SmaartRadio[4733:303] -[__NSCFString objectForKey:]: unrecognized selector sent to实例 0x618000030360
    猜你喜欢
    • 1970-01-01
    • 2015-10-14
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    相关资源
    最近更新 更多