我在读取plist文件时遇到了问题,我将Settings.plist文件保存在我的项目中(尝试了在资源文件夹中和资源文件夹之外),谷歌和stackOverFlow提供的方法都不起作用。我不知道我错过了什么。代码是:
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");我尝试了许多不同的方法来获取*plistPath,NSBundle,只需要一个带有文件名的字符串。我还尝试了NSMutabelDictionary,regulat,NSArray,NSMutableArray
似乎什么都不起作用。尝试将plist的第一行设置为根字典,但仍然没有成功。
任何帮助都将被感谢..Settings.plist有一个单一的触摸值,它的键是YES,谢谢!
编辑:下面是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>发布于 2012-02-02 11:51:41
您的plist包含一个带有一个键“Root”的顶级字典。该键的值是具有一个键“Touch”的另一个字典。
试试这个:
BOOL touch = [[[settings objectForKey:@"Root"] objectForKey:@"Touch"] boolValue];或者这样(稍微慢一点):
BOOL touch = [[settings valueForKeyPath:@"Root.Touch"] boolValue];https://stackoverflow.com/questions/9106777
复制相似问题