2010-11-21 10 views
2

Der folgende Code meldet jedes Mal "NEIN". Hilfe wäre sehr willkommen!Problem beim Lesen eines Bool-Werts von einem Plist

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> 

Antwort

3

Ich vermute, die plist-Datei nicht im aktuellen Arbeitsverzeichnis und die NSDictionary von initWithContentsOfFile: zurück ist leer oder gleich Null. Sie können diese plistDictionary, indem Sie überprüfen:

NSLog(@"%@", plistDictionary); 

Eine Lösung den vollständigen Pfad der plist-Datei angeben würde. Oder, wenn die in der plist-Datei gespeicherten Werte Voreinstellungen sind, könnten Sie NSUserDefaults verwenden.

0

Es funktionierte für mich. Es ist möglich, dass in Ihrem Fall die Datei nicht gefunden wird. In diesem Fall ist plistDictionary gleich Null und würde die Ausgabe erzeugen, die Sie sehen. Versuchen Sie, eine Prüfung hinzuzufügen, dass der Init-Aufruf Ihnen tatsächlich ein Wörterbuch zurückgegeben hat.

0

Da ein Objekt für Schlüssel kann auch eine andere Klasse sein kann boolValue BUGGIE (kann eine Ausnahme, wenn kein NSNumber Klasse erzeugen) und was auch immer lügen, wenn es sich um eine Zahl 0 oder 1 ist, das ist meine Lösung:

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

Keine Ausnahmen, und Ihnen die Wahrheit sagen!

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