Search code examples
objective-cplist

Issue reading a bool value from a plist


The following code logs "NO" every time. Help would be very appreciated!

Code:

 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];

settings.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>

Solution

  • I suspect the plist file isn't in the current working directory and the NSDictionary returned by initWithContentsOfFile: is empty or nil. You can verify this by logging plistDictionary:

    NSLog(@"%@", plistDictionary);
    

    One solution would be to specify the full path of the plist file. Or, if the values stored in the plist file are preferences, you could use NSUserDefaults.