Write/Read plist file iPhone

前端 未结 2 1096
暗喜
暗喜 2021-02-06 11:32

I have plist in my iphone app and I want to read and write an integer from my single to and form it.

I have this to read it:

 scoreData *score = [scoreD         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 12:32

    Two things:

    1. When you're setting a key in a dictionary, you're probably after setObject:forKey: instead of setValue:forKey:.

    2. Dictionaries hold objects (type id or NSString* or your own Obj-C objects), not primitive types (int, BOOL, etc).

    When you want to store a primitive type in an Obj-C collection, you can "wrap" it in an object like this:

    [plistDict setObject:[NSNumber numberWithInt:score.highScore] forKey:@"score"];
    

    And get it back like this:

    score.highScore = [[pListDict objectForKey:@"score"] intValue];
    

提交回复
热议问题