我在XCode中有一个Array,里面有Locations,每个位置都有displayName,Category等属性......
现在我想过滤那个数组,以便得到一个只包含类别“XYZ”之外的位置的数组?
我如何在XCode中执行此操作?
我的位置数组看起来像
{
[displayName: "Location1", category: "XYZ" ],
[displayName: "Location2", category: "ABC" ],
[displayName: "Location3", category: "XYZ" ],
[displayName: "Location4", category: "ABC" ],
[displayName: "Location5", category: "XYZ" ],
[displayName: "Location6", category: "ABC" ],
}
在这些中我希望位置1,3,5例如在这样的新数组中:
{
[displayName: "Location1", category: "XYZ" ],
[displayName: "Location3", category: "XYZ" ],
[displayName: "Location5", category: "XYZ" ],
}
答案 0 :(得分:1)
使用这段代码,其中mixedArray是您的初始数组,newArray是已过滤的数组,YourLocation是您的位置类。
NSArray *newArray = [mixedArray objectsAtIndexes:[mixedArray indexesOfObjectsPassingTest:^BOOL(YourLocation *location, NSUInteger idx, BOOL *stop) {
return ([location.category isEqualToString:@"XYZ"]);
}]];
希望它会对你有所帮助。