2012-02-02 173 views
0

我从plist文件中读取问题 我的Settings.plist文件保存在我的项目中(尝试进出资源文件夹) 没有任何方法谷歌和stackOverFlow必须提供工作。我不知道我错过了什么。 代码:从plist中读取

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 NSDictionary,NSArray,NSMutableArray

没有什么似乎工作。 尝试plist中第一行设置为根字典,仍然没有运气..

任何帮助将apreciated .. Settings.plist有一个单一的价值Touch和它的关键是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> 
+0

什么是'settings'后'initWithContentsOfFile值:'回报?另外,编辑你的问题并粘贴在'Settings.plist'文件的内容中。 – 2012-02-02 03:36:34

+0

如何判断它自己的设置的值? – 2012-02-02 03:43:46

+0

使用'NSLog'或调试器。 – 2012-02-02 03:49:50

回答

3

你的plist中包含顶级词典一个键,“Root”。该密钥的值是带有一个密钥的另一个字典,“Touch”。

试试这个:

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

或本(速度稍慢):

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

谢谢! 第一个工作! – 2012-02-02 03:55:15

+0

他们都做同样的事情。第二个更容易编写,但运行速度较慢。 – 2012-02-02 03:56:27