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.p开发者_StackOverflow社区list:
<?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>
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
.
It worked for me. Its possible that in your case its not finding your file, in which case plistDictionary will be nil, and that would produce the output you're seeing, try adding a check that init call actually returned you a dictionary and not nil.
Since a object for key can be also another class, boolValue can buggie (can generate an exception if not a NSNumber class) and whatever be lying if it is a number 0 or 1, this is my solution:
- (BOOL)isBooleanKey:(id)key
{
#ifndef kNullString // can be somewhere
#define kNullString @"(null)"
#endif
if (!key){
NSLog(@"WARNING:[- (BOOL)%@(id)key, \"key\" is nil]\n", NSStringFromSelector(_cmd));
return NO;
}
if ([key isKindOfClass:[NSNumber class]]) {
NSDictionary *dict = [NSDictionary dictionaryWithObject:key forKey:@"test"];
if (!dict) return NO;
NSError *err = nil;
NSPropertyListFormat fmt = NSPropertyListXMLFormat_v1_0;
id data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:&err];
if(!data) {
NSLog(@"dict is not a XMLFormat v1\n"); // anyway this can't be happen here, unless something is really bad!
}
id pl =[NSPropertyListSerialization propertyListWithData:data options:NSPropertyListMutableContainersAndLeaves format:&fmt error:&err];
#if 0
NSLog(@" err: %@", err.localizedDescription);
#endif
if(!pl) {
[NSException raise: NSParseErrorException format:@"%@\n", err];
if(![data isKindOfClass:[NSDictionary class]])
[NSException raise: NSParseErrorException
format: @"dict does not contain a property list\n"];
}
NSString* plist = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (plist.length < 1 || [plist isEqualToString:kNullString]) return NO; //kNullString is a macro -> @"(null)"
// dict has only one key, so if it's not soup is soaked bread!
if ([plist rangeOfString:@"<true/>"].location != NSNotFound
|| [plist rangeOfString:@"<false/>"].location != NSNotFound) {
// object for key is a boolean for sure (not simply a number!)
return YES;
}
}
// key is not a boolean
return NO;
}
No exceptions, and tell you the truth!
if ([self isBooleanKey:[someobject forKey:@"some key"]]]) {
// Yes
} else {
// NO
}
精彩评论