문제

내 iPhone 앱은 Key-Value 쌍을 Plist 파일로 사전에 씁니다. 기본적으로 사용자가 게임을 할 때 사용자의 점수를 저장하고 있습니다. 이것은 모두 훌륭하고 멍청하지만 앱을 실행하고 새 점수를 얻을 때마다 새로운 값이 이전 값에 대해 저장됩니다. 사용자가 파일을 다시 작성하는 대신 앱에 액세스 할 때마다 plist에 정보를 추가하려면 어떻게합니까? 나는 가장 최근의 점수뿐만 아니라 모든 점수를 유지하고 싶습니다.

암호:

-(void)recordValues:(id)sender {

    //read "propertyList.plist" from application bundle
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path
                          stringByAppendingPathComponent:@"propertyList.plist"];
    dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];

    //create an NSNumber object containing the
    //float value userScore and add it as 'score' to the dictionary.
    NSNumber *number=[NSNumber numberWithFloat:userScore];
    [dictionary setObject:number forKey:@"score"];

    //dump the contents of the dictionary to the console 
    for (id key in dictionary) {
        NSLog(@"memory: key=%@, value=%@", key, [dictionary
                                                 objectForKey:key]);
    }

    //write xml representation of dictionary to a file
    [dictionary writeToFile:@"/Users/rthomas/Sites/propertyList.plist" atomically:NO];
}
도움이 되었습니까?

해결책

키 스코어를 위해 개체를 숫자로 설정하고 있습니다.

    NSNumber *number=[NSNumber numberWithFloat:userScore];   
 [dictionary setObject:number forKey:@"score"];

이 대신에 당신이하고 싶은 것은 배열이나 무언가가있는 것입니다.

NSNumber *number=[NSNumber numberWithFloat:userScore];  
    NSMutableArray *array=[dictionary objectForKey:@"score"]
     [array addObject:number]
    [dictionary setObject:array forKey:@"score"]

이것은 당신이 요구하는 일을해야합니다

다른 팁

Daniel이 말한 것처럼 nsarray 또는 nsdictionary에서 오래된 값을 먼저로드하고 싶습니다.

당신은 컬렉션에 새 값을 추가합니다. (아마도 분류 나 뭔가를 할 수도 있습니다)

그런 다음 새 컬렉션을 디스크에 다시 작성합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top