I want to check if a string is in a const struct. The way I do it is like this:
In my MyClass.h:
extern const struct MyAttributes {
__unsafe_unretained NSString *attribute1;
__unsafe_unretained NSString *attribute2;
__unsafe_unretained NSString *attribute3;
__unsafe_unretained NSString *attribute4;
__unsafe_unretained NSString *attribute5;
} MyAttributes;
Then in my MyClass.m I have:
const struct MyAttributes MyAttributes = {
.attribute1 = @"attribute1",
.attribute2 = @"attribute2",
.attribute3 = @"attribute3",
.attribute4 = @"attribute4",
.attribute5 = @"attribute5"
};
...
- (void)someMethod
{
BOOL test1 = [self check:@"test"];
BOOL test2 = [self check:@"attribute1"];
}
- (BOOL)check:(NSString *)string
{
return [string isEqualToString:MyAttributes.attribute1] ||
[string isEqualToString:MyAttributes.attribute2] ||
[string isEqualToString:MyAttributes.attribute3] ||
[string isEqualToString:MyAttributes.attribute4] ||
[string isEqualToString:MyAttributes.attribute5];
}
Well, this works. But, is there a better way to implement - (void)check
so that, if I update MyAttributes
, I won't have to update - (void)check
?