根据另一个数组中的对象从数组中删除特定的对象 [英] Remove particular objects from an array based on objects from another array
问题描述
安装:有一个 UITableView
,它显示美国高尔夫球场的名称,街道,州等。
UITableView's
数据源是来自我的类的 NSMutableArray
对象 GolfCourse
称为 allGolfCourses $现在我喜欢从
allGolfCourses
删除所有的西海岸高尔夫球场,并创建一个新的
eastCoastGolfCourses
。我还有另外一个 NSArray
,其中字符串对象
所有西海岸州(缩写)称为 westCoastStates
但很难连接这两者。 如何迭代allGolfCourses并删除所有具有状态的对象在<$ c中找到的缩写$ c> westCoastStates 数组
$ b
westCoastStates数组:
self.westCoastStates = [NSMutableArray arrayWithObjects:
@CH,
@OR,
@WA,
nil];
GolfCourse.h
@interface GolfCourse:NSObject
@property(nonatomic,strong)NSString * longitude;
@property(nonatomic,strong)NSString * latitude;
@property(nonatomic,strong)NSString * clubName;
@property(nonatomic,strong)NSString * state;
@property(nonatomic,strong)NSString * courseInfo;
@property(nonatomic,strong)NSString * street;
@property(nonatomic,strong)NSString * city;
@property(nonatomic,strong)NSString * clubID;
@property(nonatomic,strong)NSString * phone;
@end
注意:NSString * state;包含状态缩写例如:FL
我知道如何使用单个参数来完成此操作,但不知道如何检查 westCoastStates
数组。希望你能帮到忙。
如何?
NSSet * westCoastStatesSet = [NSSet setWithArray:self.westCoastStates];
NSIndexSet * eastCoastGolfCoursesIndexSet = [allGolfCourses indexesOfObjectsPassingTest:^ BOOL(id obj,NSUInteger idx,BOOL * stop){
GolfCourse * course =(GolfCourse *)obj;
if([westCoastStatesSet containsObject:course.state]){
return NO;
}
return YES;
}];
NSArray * eastCoastGolfCourses = [allGolfCourses objectsAtIndexes:eastCoastGolfCoursesIndexSet];
更新:我相信这可以使用谓词
NSPredicate * inPredicate = [NSPredicate predicateWithFormat:@!(state IN%@),self.westCoastStates];
NSArray * eastCoastGolfCourses = [allGolfCourses filteredArrayUsingPredicate:inPredicate];
Setup: Have a UITableView
which shows US golf courses with name, street, state etc.
UITableView's
data source is a NSMutableArray
of objects from my class GolfCourse
called allGolfCourses
.
Now I like to remove all west coast golf courses from allGolfCourses
and create a new array
called eastCoastGolfCourses
. I have another NSArray
with string objects
of all west coast states (Abbreviations) called westCoastStates
but having a hard time connecting these two.
How do I iterate through allGolfCourses and remove all objects which have a state Abbreviations found in westCoastStates
array?
westCoastStates Array:
self.westCoastStates = [NSMutableArray arrayWithObjects:
@"CH",
@"OR",
@"WA",
nil];
GolfCourse.h
@interface GolfCourse : NSObject
@property (nonatomic, strong) NSString *longitude;
@property (nonatomic, strong) NSString *latitude;
@property (nonatomic, strong) NSString *clubName;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, strong) NSString *courseInfo;
@property (nonatomic, strong) NSString *street;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *clubID;
@property (nonatomic, strong) NSString *phone;
@end
Note: NSString *state; contains the state abbreviation for example: FL
I know how to do this with a single argument but don't know how to check against all strings from westCoastStates
array. Hope you can help.
How about?
NSSet* westCoastStatesSet = [NSSet setWithArray:self.westCoastStates];
NSIndexSet* eastCoastGolfCoursesIndexSet = [allGolfCourses indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
GolfCourse* course = (GolfCourse*)obj;
if ([westCoastStatesSet containsObject:course.state]) {
return NO;
}
return YES;
}];
NSArray* eastCoastGolfCourses = [allGolfCourses objectsAtIndexes:eastCoastGolfCoursesIndexSet];
Update: I believe this could be condensed with the use of predicates
NSPredicate *inPredicate = [NSPredicate predicateWithFormat: @"!(state IN %@)", self.westCoastStates];
NSArray* eastCoastGolfCourses = [allGolfCourses filteredArrayUsingPredicate:inPredicate];
这篇关于根据另一个数组中的对象从数组中删除特定的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!