How to return any type value like -performSelector?

Mr. Ming

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

Bryan Chen

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to solve errors like void' is not assignable to parameter of type '(value: [Post, any, {}[]]) => void'

How to return a value of unknown type?

How to type return value as "throw"

How to type the return value in Typescript?

How does constructor return if it doesn't have any return type?

Cocoa Scripting: Return a floating point (double) value for "any" type

How to update value in a "Any" type Object

How to unwrap an optional value from Any type?

How to convert from value with Any type to Real?

How to check if value type is Dictionary<String, Any>

How to resolve type in Kotlin when value is Any?

I am able to return value of a string type when the function return type is Promise<any> when using Async

How does "any" type work with basic types like string?

How to return rows based on one, specific value (and not with any other value)?

Return value of any()

How to refer to the type of a closure in a generic return value?

How to return a value from a function of type `LPCTSTR`?

how to check return value type in scala

How does Scala define a type of return value?

How to type return value as boolean|typeof param

How to correctly type the return value of factory method

How to force type on return value in a generic class?

How to force type on return value in a generic class?

How to type annotate the return value of a macro?

How to return string value depending on type of generic

How to set typescript type for matchPath return value

How to return a value of a type aliased in a class?

How to type to prevent a return value in Typescript

How to check the return value of the type() method in Python?