2013-07-27 70 views
0

喜,而不是我要補充一些數據的plist在**在下面的圖像格式如下像** actual i want的plist值替換添加

但實際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

您需要在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]; 

} 

否則,您只需在該數組中重複添加相同的字典。

+0

:超級老闆。你是man.one懷疑我們是否在循環中創建一個字典它會使用更多的內存嗎? – Navi

+0

是的,但除非你有很大的plist文件,否則我不會太擔心。一旦將plist寫入文件,您分配的所有內存都將被回收。我建議使用'NSDictionary'而不是'NSMutableDictionary',並使用'+(id)dictionaryWithObjects:(NSArray *)對象forKeys:(NSArray *)keys'來一次性創建它。這是因爲您已經知道鍵的數量和其中的值。 –

+0

我有plist與150個條目 – Navi