从 NSMutableArray 中删除特定项目 [英] Delete Specific Items from NSMutableArray

查看:28
本文介绍了从 NSMutableArray 中删除特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行相对简单的任务,即删除 NSMutableArray 中符合特定条件的项目.

I'm trying to perform the relatively simple task of deleting the items in an NSMutableArray that fit a certain criteria.

在这种特殊情况下,我想删除满足以下所有条件的项目:

In this particular case, I'd like to delete the items where all of the following are true:

  • 城市:西雅图
  • 州:华盛顿
  • 时间戳:超过 60 秒

我的数组的 NSLog 如下所示:

array = (
            {
            city = "Seattle";
            state = "WA";
            timestamp = 1432844384;
        },
            {
            city = "Dallas";
            state ="TX";
            timestamp = 1432844415;
        },
        {
            city = "Seattle";
            state = "WA";
            timestamp = 1432845329;
        }
    )

我已经尝试使用 NSPredicate 来过滤数组,但我认为这可能会使事情变得过于复杂.任何建议都会很棒!谢谢!

I've tried using NSPredicate to filter the array, but I think that may be overcomplicating things. Any recommendations would be great! Thank you!

推荐答案

您可以使用 indexesOfObjectsPassingTest: (查看文档)

NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    // check if obj is matching all your criteria
}

然后使用 removeObjectsAtIndexes: (参见文档)

[array removeObjectsAtIndexes:indexes];

这篇关于从 NSMutableArray 中删除特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆