I’m writing some library code, and users of the library sometimes have different opinions about which methods should use int and when to use NSNumber. I would like some way to set up a method so that either of
[myObject myMethod:5];
or
[myObject myMethod:[NSNumber numberWithInt:5]]
works the same way. In Java, autoboxing makes this relatively straightforward, but I’m not aware of any way to make this happen in objective-c. Is it possible to make this work?
Short answer: no.
Objective-C does not support function/method overloading, as in C++.
Usually, if you need to support different datatypes, you will create distinct methods.
Note that you can then convert yourself the passed value, so you don’t have to write (copy/paste) the same code.
For instance:
At the end, everything will be redirected to the
methodWithNumbermethod.