2013-10-14 110 views
-2

我有這樣的:的NSMutableArray NSDictionary的從

NSMutableArray *tableData; 
NSMutableArray *thumbnails; 
NSMutableArray *prepTime; 


NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"]; 
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
tableData = (NSMutableArray *)[dict objectForKey:@"RecipeName"]; 
thumbnails = (NSMutableArray *)[dict objectForKey:@"Thumbnail"]; 
prepTime = (NSMutableArray *)[dict objectForKey:@"PrepTime"]; 

,我想這樣做:

- (void)tableView:(UITableView *)tableView commitEditingStyle:  
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableData removeObjectAtIndex:indexPath.row]; 
    [thumbnails removeObjectAtIndex:indexPath.row]; 
    [tableData removeObjectAtIndex:indexPath.row]; 
    [tableView reloadData]; 
} 

但它失敗:

2013年10月14日16:58:22.202 SimpleTable [8421:11303] *由於未捕獲異常'NSInternalInconsistencyException',原因:' - [__ NSCFArray removeObjectAtIndex:]:發送給不可變對象的變異方法'終止應用程序' *第一擲調用堆棧: (0x1c93012 0x10d0e7e 0x1c92deb 0x1d13c4f 0x29bd 0xd0d34 0x20e97b 0x10e4705 0x182c0 0x18258 0xd9021 0xd957f 0xd9056 0x2a9f4d 0x10e4705 0x182c0 0x18258 0xd9021 0xd957f 0xd86e8 0x47cef 0x47f02 0x25d4a 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1ced 0x1c15) 的libC++ ABI .dylib:終止調用拋出異常

如何正確地將字典變成mutableArray?

+0

試試這個 tableData =(NSMutableArray *)[[dict objectForKey:@「RecipeName」] mutableCopy] –

+0

每個'objectForKey'返回什麼? – Popeye

回答

4

[[dict objectForKey:@「RecipeName」] mutableCopy]應該訣竅!

編輯:您可能要投它像這樣[(NSArray的*)[字典objectForKey:@ 「RecipeName中」] mutableCopy]

+0

你是對的,只需要等6分鐘就可以應用正確的答案。謝謝 – Cheese

2

在你的代碼是從資料表

[tableData removeObjectAtIndex:indexPath.row]; 
兩次去除對象

我應該有繼,

- (void)tableView:(UITableView *)tableView commitEditingStyle: 
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableData removeObjectAtIndex:indexPath.row]; 
    [thumbnails removeObjectAtIndex:indexPath.row]; 
    [prepTime removeObjectAtIndex:indexPath.row]; 
    [tableView reloadData]; 
} 
+1

雖然這是正確的,錯誤說「突變方法發送到不可變對象」,這意味着他有一個NSarray,並調用方法屬於一個NSMutableArray –

+0

謝謝,只是拼錯我的第三個數組 – Cheese

2

由於克里斯Gellci,這確實的伎倆:

tableData = [[dict objectForKey:@"RecipeName"] mutableCopy]; 
thumbnails = [[dict objectForKey:@"Thumbnail"] mutableCopy]; 
prepTime = [[dict objectForKey:@"PrepTime"] mutableCopy]; 
相關問題