Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 isEqualToString:@"yzx"] || [aString isEqualToString:@"xzy"] etc...){}
like image 222
evdude100 Avatar asked Feb 25 '23 01:02

evdude100


2 Answers

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.

like image 117
highlycaffeinated Avatar answered Mar 07 '23 07:03

highlycaffeinated


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
like image 29
JustSid Avatar answered Mar 07 '23 08:03

JustSid