Frage

Ich versuche, eine NSArray von einer .plist-Datei zu erstellen, aber ich erhalte immer diese Fehlermeldung:

"'NSUnknownKeyException', reason: '[<NSCFString 0x5bbca60> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Value1.'"

In der .plist-Datei, ich habe einen Schlüssel „Punkt 1“ und einen String-Wert namens namens „Value1.“ Dann im Code ich habe eine NSArray aus dieser Datei erstellt werden:

-(void)recordValues:(id)sender {

    // read "propertyList.plist" values
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"propertyList" ofType:@"plist"];
    originalArray = [[NSArray alloc] initWithContentsOfFile:plistPath];

   // dump the contents of the array to the log
    for (id key in originalArray) {
        NSLog(@"bundle: key=%@, value=%@", key, [originalArray valueForKey:key]);
    }

    //create an NSMutableArray containing the
    //user's score and the values from originalArray
    NSMutableArray *newArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:userScore], nil];
    [newArray addObjectsFromArray:array];

    //write xml representation of mutableArray back to propertyList
    [newArray writeToFile:plistPath atomically:NO];

}

    }
War es hilfreich?

Lösung

Arrays haben keine Schlüssel (sie brauchen Sie nicht, weil ihre Elemente durch ihre Indizes angesprochen werden), bestehen nur Wörterbücher von Schlüssel-Wert-Paare. Versuchen Sie:

for (id value in originalArray) {
            NSLog(@"bundle: value=%@", value);
    }

Andere Tipps

In Ihrer for Schleife, verwenden Sie originalArray wie ein Wörterbuch. Wenn die Wurzel Ihres plist ein Wörterbuch ist, sollten Sie NSDictionary verwenden, nicht NSArray es zu lesen.

Sie können NSMutableArray sofort verwenden anstelle von NSArray und konvertieren zu NSMutableArray als auch sicher sein, dass Ihre pList Array-Typ ist. Ihr Code aussehen würde:

-(void)recordValues:(id)sender {
// read "propertyList.plist" values
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"propertyList" ofType:@"plist"];
//create an NSMutableArray containing the
//user's score and the values from originalArray
NSMutableArray *array = [NSArray arrayWithContentsOfFile: path];
[newArray addObjectsFromArray:array];
//check-out the content
NSLog(@"The content of the array: %@", array);
//write xml representation of mutableArray back to propertyList
[array writeToFile:plistPath atomically:NO];
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top