1

NSMutableArray私は辞書を持っているiPhoneアプリケーションを持っています。テーブルの各行を選択するときに、使用して辞書を削除しています

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

行がタップされたときに、「from id」キーが同じすべての辞書を削除するシナリオがあります。私はこのように試しましたが、効果はありませんでした:

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

3 に答える 3

4

次を使用して、配列をその場でフィルタリングできます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];
于 2013-02-27T07:58:08.257 に答える
0

配列はオブジェクトをあるインデックスに保持し、辞書はオブジェクトとキーを保持するため、アルゴリズムは次のようになります。

ループを使用して配列からオブジェクト(辞書)を1つずつ取得し、辞書にその特定のキーがあることを確認します。
はいの場合、関数を使用してそのオブジェクトを配列から削除しますremoveObjectAtIndex

于 2013-02-27T07:47:08.137 に答える
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];
于 2013-02-27T07:51:23.180 に答える