I have multiple strings combinations that I want my isEqualtoString to find automatically. Right now, I have all combinations manually searched.
if([aString isEqualToString:@"xyz"] || [aString isEqualToString:@"zxy"] || [aString isEqualToString:@"yzx"] || [aString isEqualToString:@"xzy"] etc...){}
If you just want to know if any of them match, you can put all your candidates (xyz, zxy, ...) in an NSArray
and call containsObject:aString
on the array. Use indexOfObject:aString
if you need to know which string was matched.
You can write a NSString category that does the job:
@interface NSString (isEqualToAnyStringAddition)
- (BOOL)isEqualToAnyString:(NSString *)firstString, ... NS_REQUIRES_NIL_TERMINATION;
@end
@implementation NSString (isEqualToAnyStringAddition)
- (BOOL)isEqualToAnyString:(NSString *)firstString, ...
{
if([self isEqualToString:firstString])
return YES;
va_list arguments;
va_start(arguments, firstString);
NSString *string;
while((string = va_arg(arguments, NSString *)))
{
if([self isEqualToString:string])
{
va_end(arguments);
return YES;
}
}
va_end(arguments);
return NO;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With