1

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.

2
  • How do I iterate through allGolfCourses and remove all objects which have a state Abbreviations found in westCoastStates array? Maybe you iterate through allGolfCOurses and remove all objects which have a state abbreviation that matches one in westCoastStates. Start with while and continue from there. (Or actually for would be better.) Commented Nov 4, 2012 at 15:38
  • 1
    It's worth leveraging the enumeration abilities of NSArray, both for performance and readability purposes. Commented Nov 4, 2012 at 15:51

3 Answers 3

3

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];
Sign up to request clarification or add additional context in comments.

Comments

0

Pseudo-code:

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++;
    }
}

Comments

0

You can quickly iterate on an array like this:

[self.allGolfCourses enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    GolfCourse *currentGolfCourse = (GolfCourse *)obj;
    if(![self.westCoastStates containsObject:currentGolfCourse.state]){
        [self.eastCoastStates addObject:currentGolfCourse];
    }
}];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.