替换Plist值而不是添加

时间:2013-07-27 14:57:09

标签: ios objective-c nsarray plist nsdictionary

嗨我必须以**格式向plist添加一些数据,如下图所示** actual i want

但实际的iam就像在这里

currently wat iam getting 请帮助我的代码...

-(void)updatePlist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathOfPricePlist= [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Price.plist"];
    NSMutableDictionary *mutableDictionary=[[NSMutableDictionary alloc]init];
    NSMutableArray *finalList=[[NSMutableArray alloc]init];
    for(int i=0;i<[sharedManager.bookidList count];i++) {

        [mutableDictionary setValue:[sharedManager.sharedProductPrice objectAtIndex:i] forKey:@"price"];
        [mutableDictionary setValue:[sharedManager.bookidList objectAtIndex:i] forKey:@"booknumber"];

        NSLog(@"%@",mutableDictionary);
        [finalList addObject:mutableDictionary];
        NSLog(@"%@",finalList);
    }

    [finalList writeToFile:pathOfPricePlist atomically:YES];

}

1 个答案:

答案 0 :(得分:1)

您需要在for循环的每一步创建一个新的mutableDictionary。像这样:

-(void)updatePlist
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathOfPricePlist= [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Price.plist"];

    NSMutableArray *finalList=[[NSMutableArray alloc]init];
    for(int i=0;i<[sharedManager.bookidList count];i++) {

        NSMutableDictionary *mutableDictionary=[[NSMutableDictionary alloc]init];


        [mutableDictionary setValue:[sharedManager.sharedProductPrice objectAtIndex:i] forKey:@"price"];
        [mutableDictionary setValue:[sharedManager.bookidList objectAtIndex:i] forKey:@"booknumber"];

        NSLog(@"%@",mutableDictionary);
        [finalList addObject:mutableDictionary];
        NSLog(@"%@",finalList);
    }

    [finalList writeToFile:pathOfPricePlist atomically:YES];

}

否则,您只需在该数组中反复添加相同的字典。