There's a really strange behavior going on with float / double / CGFloat casting on the result of performSelector:
Why does this work?
BOOL property = (BOOL)[self.object performSelector:@selector(boolProperty)];
NSInteger property = (NSInteger) [self.object performSelector:@selector(integerProperty)];
And this doesn't
CGFloat property = (CGFloat) [self.object performSelector:@selector(floatProperty)];
At first I tried to do this:
CGFloat property = [[self.object performSelector:@selector(floatProperty)] floatValue];
But I ended up getting an EXC_BAD_ACCESS runtime error. I already figured out a hack to solve this issue but I would like to understand why it works with Integer and Bool and not with floating point types.
My hack:
@implementation NSObject (AddOn)
-(CGFloat)performFloatSelector:(SEL)aSelector
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:aSelector]];
[invocation setSelector:aSelector];
[invocation setTarget:self];
[invocation invoke];
CGFloat f = 0.0f;
[invocation getReturnValue:&f];
return f;
}
@end
Then:
CGFloat property = [self.object performFloatSelector:@selector(floatProperty)];
id
... perhaps castingid
as a long is safer than casting it as a float. See the NSObject protocol reference. I still wonder why the protocol is separate from the class, but whatever. – Cockneyfyid
toBOOL
andNSInteger
should not be allowed in ARC. Is this MRC then? – Casta