Navi
Navi

Reputation: 1686

Plist values are replaced instead of added

hi i have to add some data to plist in **following format like in below image** actual i want

but the actual iam getting is like here

currently wat iam getting please help my code for this is ..

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

}

Upvotes: 0

Views: 105

Answers (1)

Andrei Stanescu
Andrei Stanescu

Reputation: 6383

You need to create a new mutableDictionary on each step of the for loop. Like this:

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

}

Otherwise you simply add the same dictionary over and over again in that array.

Upvotes: 1

Related Questions