Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing BOOL to makeObjectsPerformSelector:withObject:

Tags:

iphone

nsarray

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.

like image 808
PokerIncome.com Avatar asked Mar 21 '11 07:03

PokerIncome.com


2 Answers

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]];
like image 95
Matthias Bauch Avatar answered Sep 25 '22 03:09

Matthias Bauch


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 :)

like image 42
StuFF mc Avatar answered Sep 22 '22 03:09

StuFF mc