Andrew Johnson
Andrew Johnson

Reputation: 13286

This giant if statement feels wonky... how would you do this in Objective C

for (NSString *metarComponent in self.readingComponents) {
    if ( [metarComponent hasPrefix:@"+"]
         || [metarComponent hasPrefix:@"-"]
         || [metarComponent hasPrefix:@"VC"]
         || [metarComponent hasPrefix:@"MI"]
         || [metarComponent hasPrefix:@"PR"])
         || [metarComponent hasPrefix:@"BC"])
         || [metarComponent hasPrefix:@"DR"])
         || [metarComponent hasPrefix:@"BL"])
         || [metarComponent hasPrefix:@"SH"])
         || [metarComponent hasPrefix:@"TS"])
         || [metarComponent hasPrefix:@"PZ"])
         || [metarComponent hasSuffix:@"DZ"])
         || [metarComponent hasSuffix:@"RA"])
         || [metarComponent hasSuffix:@"SN"])
         || [metarComponent hasSuffix:@"SG"])
         || [metarComponent hasSuffix:@"IC"])
         || [metarComponent hasSuffix:@"PL"])
         || [metarComponent hasSuffix:@"GR"])
         || [metarComponent hasSuffix:@"GS"])
         || [metarComponent hasSuffix:@"UP"])
         || [metarComponent hasSuffix:@"BR"])
         || [metarComponent hasSuffix:@"FG"])
         || [metarComponent hasSuffix:@"FU"])
         || [metarComponent hasSuffix:@"VA"])
         || [metarComponent hasSuffix:@"DU"])
         || [metarComponent hasSuffix:@"SA"])
         || [metarComponent hasSuffix:@"HZ"])
         || [metarComponent hasSuffix:@"PY"])
         || [metarComponent hasSuffix:@"PO"])
         || [metarComponent hasSuffix:@"SQ"])
         || [metarComponent hasSuffix:@"FC"])
         || [metarComponent hasSuffix:@"SS"])
         || [metarComponent hasSuffix:@"DS"]) {     
            [rawWeatherStrings addObject:metarComponent];
        }
}

Upvotes: 4

Views: 217

Answers (4)

I would create an NSSet of prefixes and an NSSet of suffixes, then use containsObject on these sets with the first character (also first two characters after reviewing your prefix set) and last two characters pulled out of the string in question, to check against the sets. The lookup will be very quick.

Upvotes: 5

LandonSchropp
LandonSchropp

Reputation: 10234

NSArray *prefixes = [NSArray arrayWithObjects: @"+", @"-", ..., @"PZ", nil];

NSArray *suffixes = [NSArray arrayWithObjects: @"DZ", @"RA", ..., @"DS", nil];

for (NSString *metarComponent in self.readingComponents) {
    for (NSString *prefix in prefixes)
        if ( [metarComponent hasPrefix:prefix])
             [rawWeatherStrings addObject:metarComponent];
    for (NSString *suffix in suffixes)
        if ( [metarComponent hasSuffix:suffix])
             [rawWeatherStrings addObject:metarComponent];
}

Upvotes: 0

Gavin H
Gavin H

Reputation: 10482

Build a constant table of the prefix/suffix and a flag indicating which it is, then iterate over the table to check if it has the prefix or suffix.

Upvotes: 0

detunized
detunized

Reputation: 15289

An array of prefixes and array of suffixes and two for loops should do it.

Upvotes: 1

Related Questions