从plist读取bool值的问题

时间:2010-11-21 03:57:37

标签: objective-c plist

以下代码每次都会记录“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];

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>

3 个答案:

答案 0 :(得分:3)

我怀疑plist文件不在当前工作目录中,NSDictionary返回的initWithContentsOfFile:为空或为零。您可以通过记录plistDictionary

来验证这一点
NSLog(@"%@", plistDictionary);

一种解决方案是指定plist文件的完整路径。或者,如果plist文件中存储的值是首选项,则可以使用NSUserDefaults

答案 1 :(得分:0)

它对我有用。它可能在你的情况下它没有找到你的文件,在这种情况下plistDictionary将是nil,这将产生你看到的输出,尝试添加一个检查,init调用实际上返回一个字典而不是nil。

答案 2 :(得分:0)

由于key的对象也可以是另一个类,boolValue可以buggie(如果不是NSNumber类,可以生成异常),如果它是0或1,那么这就是说谎,这是我的解决方案:

- (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;
}

没有例外,说实话!

if ([self isBooleanKey:[someobject forKey:@"some key"]]]) {
   // Yes
} else {
   // NO
}