2013-02-27 29 views
1

我有一個IPhone應用程序,其中有一個NSMutableArray字典。當選擇表的每一行,我用如何從包含特定鍵的NSmutablearray中刪除所有字典?

if (indexPath.row <[self.newmessagearrays count]) 
{ 
    [messagearrays removeObjectAtIndex:indexPath.row]; 
} 

我有,我要刪除所有在我的「從標識」鍵相同的字典的情景,當行刪除字典被挖掘。我想這樣的,但沒有效果:

NSPredicate * pr = [NSPredicate predicateWithFormat:@"userid == %@",fromidstring]; 
[messagearrays filterUsingPredicate:pr]; 

回答

0

陣列保持物體在一些指標,字典保持對象和鑰匙 所以你的算法應該是像數組一個由該

獲取對象(字典)一個使用循環,檢查你的字典是否有特定的鍵。

NSPredicate * pr = [NSPredicate predicateWithFormat:@"containsKey == SOME_KEY"]; 
[messageArrays filterUsingPredicate:pr]; 

更新

響應於編輯/澄清:
如果是,則使用函數removeObjectAtIndex

0
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet]; 
NSDictionary *item; 
NSUInteger index = 0; 

for (item in yourArrayOfNSDictionaries) { 
    if ([dictionary objectForKey:theKeyYouWantToCheckFor] != nil) [discardedItems addIndex:index]; 
    index++; 
} 

[originalArrayOfItems removeObjectsAtIndexes:discardedItems]; 
4

可以使用NSPredicate過濾代替陣列從陣列中移除該對象:

NSPredicate * pr = [NSPredicate predicateWithBlock: 
    ^(id dictionary, NSDictionary * bindings) { 
    return (BOOL)![[dictionary objectForKey:@"fromid"] isEqual:@"190"]; 
}]; 
[messageArrays filterUsingPredicate:pr]; 
+0

我需要刪除字典中的fromid鍵是一個特定值的所有字典,比如說190 ... – hacker 2013-02-27 08:20:57

+0

這不是你的答案中的情況,我懷疑 – hacker 2013-02-27 08:22:51

+2

@hacker不,這不清楚我(在原始問題中)。上面的代碼將刪除數組中所有包含鍵「SOME_KEY」的值(* any * value)的字典。您在澄清中描述的謂詞可以寫爲:'NSPredicate * pr = [NSPredicate predicateWithBlock:^(id evaluateObject,NSDictionary * bindings){return(BOOL)![[evaluateObject objectForKey:@「fromid」] isEqual:@「190 「];}];' – justin 2013-02-27 08:42:55

相關問題