This gigantic if statement seems awkward ... how would you do it 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]; } } 
+4
source share
4 answers

I would create NSSet prefixes and NSSet suffixes, and then use containsObject on these sets with the first character (also the first two characters after looking at your prefix set) and the last two characters displayed on the line in question, check the sets. Search will be very fast.

+5
source

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

+1
source

Create a table of prefix / suffix constants and a flag indicating what it is, then scroll through the table to see if it has a prefix or suffix.

0
source
 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]; } 
0
source

Source: https://habr.com/ru/post/1337995/


All Articles