I want to pass BOOL to [NSArray makeObjectsPerformSelector:withObject:]
as a parameter. E.g.
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: NO];
The above code won't work because withObject only accepts id.
What's the right way to do it?
I seen some code with this:
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanTrue];
[buttons makeObjectsPerformSelector:@selector(setEnabled:) withObject: (id)kCFBooleanFalse];
This works fine on 4.2 simulator but fails on 4.2 iphone.
You could write a UIButton (or even UIView) category that allows you to use setEnabled: with an object.
@interface UIButton(setEnabledWithObject)
- (void)setEnabledWithNSNumber:(NSNumber *)bNum;
@end
@implementation UIButton(setEnabledWithObject)
- (void)setEnabledWithNSNumber:(NSNumber *)bNum {
[self setEnabled:[bNum boolValue]];
}
@end
and then you could use
[buttons makeObjectsPerformSelector:@selector(setEnabledWithNSNumber:) withObject:[NSNumber numberWithBool:NO]];
[buttons makeObjectsPerformSelector:@selector(setEnabledWithNSNumber:) withObject:[NSNumber numberWithBool:YES]];
I recall one had to do something else than just withObject:@YES
but since I can't find it anymore I figured out it works as well with
[buttons enumerateObjectsUsingBlock:^(NSButton *item, NSUInteger idx, BOOL *stop)
{[item setEnabled:YES];}];
Or the faster/older/readabler :) way:
for (NSButton *item in buttons) {[item setEnabled:YES];};
One should know that enumerateObjectsUsingBlock isn't particularily fast, but it shouldn't be a huge killer here anyways :) If you want fast you can also do that with a for (;;)
block, sure :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With