iOS-比较数组中的两个数组和对象-逻辑问题 [英] iOS - Comparing 2 Arrays and Objects in an array - Logic Issue
问题描述
我有一个 NSArray
,其中包含 Person
对象.
此人员对象包含以下内容;
>名称>年龄>学校>地址>电话号码
稍后,我将为此人对象设置值,例如 person.Name = @"Jemmy";
(但我将不会设置其他属性,例如年龄,学校等)./p>
我有一个名为 personArray
的 NSArray
,其中包含1000个人对象记录.现在,我需要过滤掉包含 Name
Jemmy
的所有对象.我该怎么办?
我当时想做的是
NSMutableArray * arrayThatContainAllPersonObjects = [NSMutableArray arrayWithArray:personArray];[arrayThatContainAllPersonObjects removeObjectsInArray:arrayWeAddedTheName];
但是,我将得到的是一个没有筛选结果的数组.无论如何,这可能不是正确的方法.我相信我们可以使用 NSSets
, UNIONS
来解决此问题.
注意:有人可能会说这是一个重复的问题,但是我对此进行了很多搜索.
您想将NSPredicate与NSArray的 filteredArrayUsingPredicate
一起使用.像这样:
NSArray * filteredArray = [personArrayfilteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = \" Jemmy \"]]];
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.
You want to use an NSPredicate with NSArray's filteredArrayUsingPredicate
. Something like this:
NSArray *filteredArray = [personArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = \"Jemmy\""]];
这篇关于iOS-比较数组中的两个数组和对象-逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!