I want to define a min and max methods in a Utils class.
@interface Utils
int min(int a, int b);
int max(int a, int b);
@end
But I don't want to have named parameters. It would be a too heavy notation. I wanted to use the C-style definition. But then [Utils min(a, b)]
as a call doesn't work. What is my problem?
Thanks in advance for any help
It is already defined as a macro.
You dont need to redefine these ones.
In a template file named "XXIntegerMath.h" drop this...
Then in your objective-c class ...
It doesn't suffer from the problems described by Regexident.
This is probably not a good idea for this particular application, but it is possible to write Objective-C methods with parameters “without names”, or rather with zero-length names:
(The selector would be
@selector(min::)
.)There's a serious security issue with the solution posted by Brandon Bodnár (which by the time of this writing is marked as a valid solution).
Issue described here: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Min-and-Max.html And the (valid & secure) solution to it: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Typeof.html
Check it out yourself:
Console log:
So never ever use the naive implementation as seen in the code above (and as suggested by Brandon Bodnár, sorry buddy ;) ) if you want to avoid worst cases like these.
Since you aren't using the OS X Implementation of objective-c, you may not have access to the predefined MIN and MAX macros.
You can define these yourself as
There is probably a better way to define them, but these will create the simple macros for your use. You can add them into any common .h file that your classes normally share.
Objective-C class methods use named parameters, period. That's just the way it is.
Why not make it a global, free function? You shouldn't need a Utils class for this kind of thing.
If you don't want to clutter the global namespace, you could use Objective-C++ (rename all .m files to .mm) and put it in a namespace.