You may want to take a look at the Objective-C runtime functions, especially objc_msgSend_fpret
.
double objc_msgSend_fpret( id self, SEL op, ... )
Which sends a message with a floating-point return value to an instance of a class.
The performSelector
methods use objc_msgSend
, which returns an id
type.
For instance:
double res = objc_msgSend_fpret( obj, @selector( blah ) );
You'll need to import this objc runtime header:
#import <objc/message.h>
EDIT
By the way, here's the link to the ObjC runtime reference:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html
EDIT 2 - IMPORTANT
objc_msgSend_fpret
is implemented in different ways, depending on the CPU architecture (basically, i386 or x86_64).
As I said in a comment, those functions are implemented using assembly, so their implementations depends on the CPU architecture.
Under the x86_64 architecture, this function returns a long double
.
This is why it fails (and returning NAN) when you assign it to a double
.
Also note that there is an objc_msgSend_fp2ret function.
So, basically, my previous example will not work:
double x = objc_msgSend_fpret( obj, @selector( blah ) );
printf( "Val: %f\n", x );
As you noticed, it will print 'NAN'.
To make it work, you'll have to do it this way:
long double x = objc_msgSend_fpret( obj, @selector( blah ) );
printf( "Val: %Lf\n", x );
Here's a working example:
http://www.eosgarden.com/uploads/misc/fp.m
Compile it using:
gcc -Wall -framework Foundation -o fp fp.m
NSInteger
? Why notNSNumber
? – Foraminifer