The return-type of -performSelector
method is id
, and I found this in Apple document:
For methods that return anything other than an object, use NSInvocation.
But the following code works well:
BOOL boolValue = (BOOL)[self performSelector:@selector(boolValue)];
It can return id
, BOOL
, NSInteger
, etc. I want to know how to do that? Because casting a BOOL
or NSInteger
to id
in the return statement caused an error:
Cast of 'NSInteger' (aka 'long') to 'id' is disallowed with ARC
Thanks in advance!
---- edit ----
Thank you for the answers.
I know it is not good to do that and I also know how to use NSInvocation, I just want to know how does -performSelector
method implemented.
---- edit ----
Finally, I found it ...
https://opensource.apple.com/source/objc4/objc4-706/runtime/NSObject.mm
In addition to use NSInvocation
as said in other answers, you can also use call the implementation function directly if you know the type at compile-time.
SEL sel = @selector(boolValue);
IMP imp = [self methodForSelector:sel];
BOOL value = ((BOOL (*)(id, SEL))imp)(self, sel);
You have to cast imp
to the correct type, otherwise it is undefined behavior and crash if you are lucky.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments