0

嗨,我必须以**以下格式向 plist 添加一些数据,如下图** 实际我想要

但实际得到的就是这里

目前我正在接受 请帮助我的代码是..

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

}
4

1 回答 1

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

}

否则,您只需在该数组中一遍又一遍地添加相同的字典。

于 2013-07-27T15:00:37.207 回答