Reputation: 13286
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
Reputation: 75058
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
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
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
Reputation: 15289
An array of prefixes and array of suffixes and two for
loops should do it.
Upvotes: 1