2010-11-21 53 views
2

以下代碼每次都會記錄「否」。幫助將非常感激!從plist讀取bool值問題

代碼:

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

我懷疑plist文件不在當前工作目錄,並通過initWithContentsOfFile:返回NSDictionary爲空或零。您可以登錄plistDictionary驗證這一點:

NSLog(@"%@", plistDictionary); 

一個解決方案是指定plist文件的完整路徑。或者,如果存儲在plist文件中的值是首選項,則可以使用NSUserDefaults

0

它爲我工作。它可能在你的情況下,它沒有找到你的文件,在這種情況下,plistDictionary將是零,這將產生你看到的輸出,嘗試添加一個檢查,調用init實際返回給你一個字典,而不是零。

0

由於用於密鑰的對象可以是也另一個類,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 
}