简体   繁体   English

从plist读取

[英]Reading from plist

I'm having problem reading from a plist file I have the Settings.plist file saved in my project (tried both in and out of the Resources folder) And none of the method google and stackOverFlow has to offered worked. 我从plist文件读取时遇到问题,我在项目中保存了Settings.plist文件(尝试在Resources文件夹中进行尝试),而google和stackOverFlow所提供的方法均无法使用。 I have no idea what I'm missing. 我不知道我在想什么。 the code is: 代码是:

    NSString *pListPath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
    NSMutableDictionary *settings = [[[NSMutableDictionary alloc] initWithContentsOfFile:pListPath] retain];
    BOOL touch = [[settings objectForKey:@"Touch"] boolValue];
    NSLog(@"%@",touch ? @"TOUCH IS YES" : @"TOUCH IS NO");

I've tried many different methods of getting the *plistPath, NSBundle, just a string with the file name. 我尝试了多种不同的方法来获取* plistPath,NSBundle,只是带有文件名的字符串。 also tried NSMutabelDictionary, regulat NSDictionary, NSArray, NSMutableArray 还尝试了NSMutabelDictionary,regulat NSDictionary,NSArray,NSMutableArray

Nothing seems to work. 似乎没有任何作用。 Tried setting the plist first line into a Root dictionary, still no luck.. 尝试将plist的第一行设置为Root词典,仍然没有运气。

Any help would be apreciated.. Settings.plist has a single value Touch and its key is YES Thanks alot! 任何帮助将不胜感激。Settings.plist具有单个值Touch,其键是YES非常感谢!

EDIT: Here's the plist: 编辑:这是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>Root</key>
    <dict>
        <key>Touch</key>
        <true/>
    </dict>
</dict>
</plist>

Your plist contains a top-level dictionary with one key, “ Root ”. 您的plist包含一个带有“ Root ”键的顶级词典。 The value for that key is another dictionary with one key, “ Touch ”. 该键的值是带有一个键“ Touch ”的另一本词典。

Try this: 尝试这个:

BOOL touch = [[[settings objectForKey:@"Root"] objectForKey:@"Touch"] boolValue];

or this (slightly slower): 或(稍微慢一点):

BOOL touch = [[settings valueForKeyPath:@"Root.Touch"] boolValue];

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

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