0

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

}
Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64
Navi
  • 1,696
  • 2
  • 17
  • 36

1 Answers1

1

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.

Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64
  • :super boss. You are the man.one doubt if we create a dictionary inside for loop it will use more memory right? – Navi Jul 27 '13 at 16:24
  • Yes it will, but I wouldn't worry too much about it unless you have really big plist files. Once you write the plist to the file, all the memory you allocated will be reclaimed. I would suggest using `NSDictionary` instead of `NSMutableDictionary`, and use `+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys` to create it in one shot. This is because you already know the number of keys and the values in it. – Andrei Stanescu Jul 27 '13 at 17:11
  • i have plist with 150 entries – Navi Jul 27 '13 at 17:39
  • Then I don't think it's not a problem using dictionaries – Andrei Stanescu Jul 27 '13 at 18:08
  • ok.thanks do u know how can i convert a currency like $10 to INR 590 ?? – Navi Jul 27 '13 at 18:09
  • This is beyond the scope of this question and there are similar questions here on SO. Here is one of them: http://stackoverflow.com/questions/4873873/realtime-currency-webservice – Andrei Stanescu Jul 27 '13 at 18:59