以下代码每次都会记录“NO”。非常感谢您的帮助!
代码:
NSString *filePath = @"settings.plist";
NSDictionary* plistDictionary = [[NSDictionary alloc] initWithContentsOfFile:filePath];
if ([[plistDictionary objectForKey:@"hideToolBarInDetailedView"] boolValue] == YES) {
detailedView.hidesBottomBarWhenPushed = YES;
NSLog(@"YES");
} else {
detailedView.hidesBottomBarWhenPushed = NO;
NSLog(@"NO");
}
[plistDictionary release];
设置.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>hideToolBarInDetailedView</key>
<true/>
</dict>
</plist>
最佳答案
我怀疑 plist 文件不在当前工作目录中,并且 initWithContentsOfFile:
返回的 NSDictionary
为空或为零。您可以通过记录 plistDictionary
来验证这一点:
NSLog(@"%@", plistDictionary);
一种解决方案是指定 plist 文件的完整路径。或者,如果存储在 plist 文件中的值是首选项,您可以使用 NSUserDefaults
。
关于objective-c - 从 plist 读取 bool 值时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4236363/