isEqualToString Combinations
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 isEqualToStri开发者_运维知识库ng:@"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
精彩评论