문제

.plist 파일에서 nsarray를 만들려고하지만이 오류가 계속됩니다.

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

.plist 파일에는 "Item 1"이라는 키와 "value1"이라는 문자열 값이 있습니다. 그런 다음 코드에서 해당 파일에서 nsarray가 생성됩니다.

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

}

    }
도움이 되었습니까?

해결책

배열에는 키가 없으며 (요소가 지수로 주소되기 때문에 필요하지 않음) 사전 만 키 값 쌍으로 구성됩니다. 노력하다:

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

다른 팁

당신의 for 루프, 당신은 사용하고 있습니다 originalArray 사전처럼. Plist의 근본이 사전 인 경우 사용해야합니다. NSDictionary, 아니다 NSArray 읽으려면.

NSARRAY 대신 NSMUTABLEARRAY를 즉시 사용하여 NSMutableARRAY로 변환 할 수있을뿐만 아니라 PLIST가 배열 유형인지 확인하십시오. 코드는 다음과 같습니다.

-(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];
}
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top