iOS - 比较 2 个数组和数组中的对象 - 逻辑问题
我有一个包含 Person
对象的 NSArray
。
这个人对象包含以下内容;
> Name
> Age
> School
> Address
> Telephone_Number
稍后我将为此 person 对象设置值,例如 person.Name=@"Jemmy";
(但我不会设置其他属性,年龄,学校等)。
我有一个名为 personArray
的 NSArray,它包含 1000 条人员对象记录。现在我需要过滤掉包含 Name
Jemmy
的所有对象。我该怎么做?
我想做的是;
NSMutableArray *arrayThatContainAllPersonObjects = [NSMutableArray arrayWithArray:personArray];
[arrayThatContainAllPersonObjects removeObjectsInArray:arrayWeAddedTheName];
但是,我将得到的是一个没有我的过滤结果的数组。无论如何,这可能不是正确的方法。我相信我们可以使用 NSSets、UNIONS 来解决这个问题。
注意:有些人可能会说这是一个重复的问题,但我对此进行了很多搜索。
I have an NSArray
which contains Person
objects.
This person object contains the following;
> Name
> Age
> School
> Address
> Telephone_Number
Later on i will be setting values to this person object, like person.Name=@"Jemmy";
(but i will not be setting other attributes, Age, School etc.).
I have an NSArray
called personArray
, and it contains 1000 person object records in it. Now i need to Filter out all the objects that contains the Name
Jemmy
. How can i do this ?
What i was thinking of doing is;
NSMutableArray *arrayThatContainAllPersonObjects = [NSMutableArray arrayWithArray:personArray];
[arrayThatContainAllPersonObjects removeObjectsInArray:arrayWeAddedTheName];
But, what i will get is, an array that doesn't have my filter results. Anyway this might not be the correct approach. I believe we could use NSSets
, UNIONS
to solve this.
note:Some might say that this is a duplicate question, but i have searched so much on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您希望将 NSPredicate 与 NSArray 的
filteredArrayUsingPredicate
一起使用。像这样的事情:You want to use an NSPredicate with NSArray's
filteredArrayUsingPredicate
. Something like this:如果 Jemmy 都是相同的,那么简单的选择是
如果情况并非如此(他们被称为 Jemmy - 但有不同的学校或其他什么),那么您将陷入
或与块/迭代类似。谓词可以用以下内容构造:
或
查阅谓词页面以获取更多详细信息。
If the Jemmy's are all identical the easy option is
If that is not the case (they are called Jemmy - but have different schools or whatever) you are down to
or similar with a block/iteration. The predicate can be constructed with something like:
or
consult the predicate page for more details.
尝试使用 NSArray 的
filteredArrayUsingPredicate:
函数并编写自定义 谓词 来评估您的对象 大批。Try using
NSArray's
filteredArrayUsingPredicate:
function and write a custom predicate to evaluate the objects in your array.