我想检查字符串是否在 const 结构中。我这样做的方式是这样的:
在我的 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;
然后在我的 MyClass.m 我有:
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];
}
嗯,这行得通。但是,有没有更好的方法来实现- (void)check
,如果我更新MyAttributes
,我就不必更新了- (void)check
?