Iphone 如何从包含特定键的NSmutablearray中删除所有字典?
我有一个IPhone应用程序,其中有一个字典的Iphone 如何从包含特定键的NSmutablearray中删除所有字典?,iphone,ios,ipad,Iphone,Ios,Ipad,我有一个IPhone应用程序,其中有一个字典的NSMutableArray。选择表的每一行时,我使用 if (indexPath.row <[self.newmessagearrays count]) { [messagearrays removeObjectAtIndex:indexPath.row]; } 数组将对象保留在某个索引中,字典保留对象和键 所以你的算法应该是这样的 使用循环逐个从数组中获取对象(字典),检查字典是否具有该特定键。 如果是,请使用函数removeO
NSMutableArray
。选择表的每一行时,我使用
if (indexPath.row <[self.newmessagearrays count])
{
[messagearrays removeObjectAtIndex:indexPath.row];
}
数组将对象保留在某个索引中,字典保留对象和键 所以你的算法应该是这样的 使用循环逐个从数组中获取对象(字典),检查字典是否具有该特定键。
如果是,请使用函数
removeObjectAtIndex
从数组中删除该对象。数组将对象保留在某个索引中,字典将对象和键保留在某个索引中
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];
所以你的算法应该是这样的
使用循环逐个从数组中获取对象(字典),检查字典是否具有该特定键。如果是,请使用函数
removeObjectAtIndex
将该对象从数组中删除。您可以使用NSPredicate
就地筛选数组:
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];
NSPredicate * pr = [NSPredicate predicateWithFormat:@"containsKey == SOME_KEY"];
[messageArrays filterUsingPredicate:pr];
更新
针对编辑/澄清:
NSPredicate * pr = [NSPredicate predicateWithBlock:
^(id dictionary, NSDictionary * bindings) {
return (BOOL)![[dictionary objectForKey:@"fromid"] isEqual:@"190"];
}];
[messageArrays filterUsingPredicate:pr];
您可以使用
NSPredicate
就地筛选阵列:
NSPredicate * pr = [NSPredicate predicateWithFormat:@"containsKey == SOME_KEY"];
[messageArrays filterUsingPredicate:pr];
更新
针对编辑/澄清:
NSPredicate * pr = [NSPredicate predicateWithBlock:
^(id dictionary, NSDictionary * bindings) {
return (BOOL)![[dictionary objectForKey:@"fromid"] isEqual:@"190"];
}];
[messageArrays filterUsingPredicate:pr];
我需要删除字典中fromid键为特定值的所有字典,比如说190…答案i中不是这样suspect@hacker不,我不清楚(在最初的问题中)。上面的代码只会删除数组中包含键
SOME\u键
的值(任意值)的所有字典。您在说明中描述的谓词可以写成:NSPredicate*pr=[NSPredicate predicateWithBlock:^(id evaluatedObject,NSDictionary*绑定){return(BOOL)![[evaluatedObject objectForKey:@“fromid”]isEqual:@“190”;}]代码>我需要删除字典中fromid键为特定值的所有字典,比如说190…答案i中的情况并非如此suspect@hacker不,我不清楚(在最初的问题中)。上面的代码只会删除数组中包含键SOME\u键
的值(任意值)的所有字典。您在说明中描述的谓词可以写成:NSPredicate*pr=[NSPredicate predicateWithBlock:^(id evaluatedObject,NSDictionary*绑定){return(BOOL)![[evaluatedObject objectForKey:@“fromid”]isEqual:@“190”;}]代码>