使用makeObjectsPerformSelector:withObject:使用false布尔值 [英] Using makeObjectsPerformSelector:withObject: with a false boolean
问题描述
我有一个 UITextField
数组 _fields
。我想要能够立刻向他们发出消息,让它们突出显示
,然后再做相同的操作,使突出显示
属性为 NO
。这部分代码的工作原理。
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
, and then do the same to turn that highlighted
property to NO
. This part of the code works.
[fields makeObjectsPerformSelector:@selector(setHighlighted:) withObject:@YES];
但这部分不会;我不能让它做任何事情。
This part, however, does not; I can't get it to do anything.
[fields makeObjectsPerformSelector:@selector(setHighlighted:) withObject:@NO];
但这样做有效。
for (UITextField *field in fields) {
field.highlighted = NO;
}
我本来希望使用 makeObjectsPerformSelector:withObject:
消息,但我没有得到太多的爱与 @NO
。
What gives? I would've liked to have used the makeObjectsPerformSelector:withObject:
message, but I'm not getting much love with @NO
. Can someone explain this behavior to me, or tell me if I'm doing something wrong?
推荐答案
setHighlighted:
方法采用 BOOL
的类型。这不是对象类型。因此,您不能使用 makeObjectsPerformSelector:withObject:
方法。
The setHighlighted:
method takes a type of BOOL
. This is not an object type. Therefore you can't use the makeObjectsPerformSelector:withObject:
method.
$ c> @YES ,因为您正在传递指向 BOOL
参数的对象的指针。非零值被视为一个 YES
值。当你传递 @NO
时,你也传递一个指针。由于它也是一个非零值,它也被视为一个 YES
值。
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.
通过将 nil
传递给 withObject:
NO >参数。 nil
值将为0,与 NO
相同。
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
.
但是这些是kludges。请改用循环方法。
But these are kludges. Use the loop approach instead.
这篇关于使用makeObjectsPerformSelector:withObject:使用false布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!