根据另一个数组中的对象从数组中删除特定的对象

设置:有一个UITableView显示美国高尔夫球场名称,街道,国家等UITableView's数据源是从我的课GolfCourse高尔夫球场的对象的NSMutableArray

现在我喜欢从所有的高尔夫球场去除所有西海岸高尔夫球场,并创build一个名为东海岸高尔夫球场的新array 。 我有另一个NSArray与所有西海岸国家(缩写)的string objects称为westCoastStates但很难连接这两个。

我如何遍历allGolfCourses并删除所有在westCoastStates数组中find状态缩写的对象?

westCoastStatesarrays:

 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 *状态; 包含州的缩写,例如:FL

我知道如何用一个参数来做到这一点,但不知道如何检查westCoastStates数组中的所有string。 希望你能帮忙。

怎么样?

 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]; 

伪代码:

 for (int i = 0; i < allGolfCourses.length;) { Course* course = [allGolfCourses objectAtIndex:i]; if (<is course in one of the "bad" states?>) { [allGolfCourse removeObjectAtIndex:i]; } else { i++; } } 

你可以像这样快速迭代一个数组:

 [self.allGolfCourses enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { GolfCourse *currentGolfCourse = (GolfCourse *)obj; if(![self.westCoastStates containsObject:currentGolfCourse.state]){ [self.eastCoastStates addObject:currentGolfCourse]; } }];