Using makeObjectsPerformSelector:withObject: with a false boolean

前端 未结 3 1116
谎友^
谎友^ 2021-01-19 05:45

I\'ve got an array of UITextField objects called _fields. I want to be able to message them all at once to set them to be highlighted,

相关标签:
3条回答
  • 2021-01-19 05:52

    rmaddy's answer explains why using makeObjectsPerformSelector:withObject: won't work.

    You can do this most succinctly by using KVC:

    [fields setValue:@NO forKey:@"hidden"];
    

    This works because NSArray passes the setValue:forKey: message through to each of its elements, and KVC properly unwraps the boxed value when the property's type is primitive.

    0 讨论(0)
  • 2021-01-19 06:03

    You should try using blocks because setHighlighted takes a BOOL as a parameter and not a pointer (NSNumber *) :

    [fields enumerateObjectsUsingBlock:^(UITextField *obj, NSUInteger idx, BOOL *stop) {
        obj.highlighted = YES; // or NO
    }];
    
    0 讨论(0)
  • 2021-01-19 06:04

    The setHighlighted: method takes a type of BOOL. This is not an object type. Therefore you can't use the makeObjectsPerformSelector:withObject: method.

    It seems to work when passing @YES because you are passing a pointer to an object to the BOOL parameter. The non-zero value gets treated like a YES value. When you pass @NO you are also passing a pointer. Since it is also a non-zero value, it also gets treated like a YES value.

    You may get the desired effect of NO by passing nil to the withObject: parameter. The nil value will be 0 which is the same value as NO.

    But these are kludges. Use the loop approach instead.

    0 讨论(0)
提交回复
热议问题