0

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?

yoninja
  • 1,952
  • 2
  • 31
  • 39

2 Answers2

1

You can convert it to an Objective-C array and then see if it contains the string you're looking for:

- (BOOL) check: (NSString*) string {
    // I renamed the variable MyAttributes to myAttributes, following naming conventions
    __unsafe_unretained id* ptr;
    struct MyAttributes* attrPtr= &myAttributes;
    memcpy(&ptr, &attrPtr, sizeof(id*));
    NSArray* array= [NSArray arrayWithObjects: ptr count: sizeof(MyAttributes)/sizeof(NSString*)];
    return [array containsObject: string];
}

The C-style way is to treat the struct as a C-array:

- (BOOL)check:(NSString *)string {
    BOOL result= NO;
    // I renamed the variable MyAttributes to myAttributes, following naming conventions
    NSString* __unsafe_unretained * strPtr;
    struct MyAttributes* ptr= &myAttributes;
    memcpy(&strPtr, &ptr, sizeof(NSString**));
    for(size_t i=0; i<sizeof(MyAttributes)/sizeof(NSString*) && !result;i++) {
        result= [string isEqualToString: strPtr[i] ];
    }
    return result;
}

PS: I used memcpy to avoid bridge-casting, since the strings are already retained in myAttributes.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
0

you can dig in the direction of keeping struct items in NSArray

Possible answer:

Cocoa structs and NSMutableArray

then in - check method use containsObject: or array iteration to check if string is there.

Community
  • 1
  • 1
Injectios
  • 2,777
  • 1
  • 30
  • 50
  • - (BOOL)containsObject:(id)anObject Description Returns a Boolean value that indicates whether a given object is present in the array. This method determines whether anObject is present in the array by sending an isEqual: message to each of the array’s objects (and passing anObject as the parameter to each isEqual: message). – Voda Ion Sep 26 '13 at 11:20
  • well... yes.. the main idea was: converting to array – Injectios Sep 26 '13 at 11:22
  • I guess this solution actually stores the struct as one object into the array. What could be the way to store the contents of the struct as individual objects to an array? – yoninja Sep 26 '13 at 11:53
  • http://stackoverflow.com/questions/6212815/how-can-i-create-an-nsmutablearray-of-structs – Injectios Sep 26 '13 at 11:56