C语言 您将如何在 obj-c 中定义一个简单的“min”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2125126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would you define a simple "min" method in obj-c
提问by Fred Polack
I want to define a min and max methods in a Utils class.
我想在 Utils 类中定义 min 和 max 方法。
@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?
但我不想有命名参数。这将是一个过于沉重的符号。我想使用 C 风格的定义。但后来[Utils min(a, b)]因为通话不起作用。我的问题是什么?
Thanks in advance for any help
在此先感谢您的帮助
采纳答案by Brandon Bodnar
Since you aren't using the OS X Implementation of objective-c, you may not have access to the predefined MIN and MAX macros.
由于您没有使用 Objective-c 的 OS X 实现,您可能无法访问预定义的 MIN 和 MAX 宏。
You can define these yourself as
您可以将这些自己定义为
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
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.
可能有更好的方法来定义它们,但这些将创建简单的宏供您使用。您可以将它们添加到您的类通常共享的任何公共 .h 文件中。
回答by Mongus Pong
It is already defined as a macro.
它已经被定义为一个宏。
MIN(a, b)
MAX(a, b)
You dont need to redefine these ones.
您不需要重新定义这些。
回答by Regexident
There's a serious issue with the solution posted by Brandon Bodnár (which by the time of this writing is marked as a valid solution).
Brandon Bodnár 发布的解决方案存在严重问题(在撰写本文时,该解决方案已被标记为有效解决方案)。
Issue described here: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Min-and-Max.htmlAnd the (valid & secure) solution to it: http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Typeof.html
问题说明如下:http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Min-and-Max.html而(有效和安全的)解决它:HTTP://gcc.gnu。 org/onlinedocs/gcc-3.4.6/gcc/Typeof.html
Check it out yourself:
自己检查一下:
#include <stdio.h>
#define NAIVE_MAX(a,b) (a > b ? a : b)
#define NAIVE_MIN(a,b) (a < b ? a : b)
#if !defined MAX
#define MAX(a,b) \
({ __typeof__ (a) __a = (a); \
__typeof__ (b) __b = (b); \
__a > __b ? __a : __b; })
#endif
#if !defined MIN
#define MIN(a,b) \
({ __typeof__ (a) __a = (a); \
__typeof__ (b) __b = (b); \
__a < __b ? __a : __b; })
#endif
int main (int argc, const char * argv[]) {
int a = 3;
int b = 5;
#pragma mark NON-FATAL CASES:
printf("NAIVE_MAX(%d, %d) => %d\n", a, b, NAIVE_MAX(a, b));
printf("NAIVE_MIN(%d, %d) => %d\n", a, b, NAIVE_MIN(a, b));
printf("MAX(%d, %d) => %d\n", a, b, MAX(a, b));
printf("MIN(%d, %d) => %d\n", a, b, MIN(a, b));
printf("\nEverything fine so far...\n\n");
#pragma mark FATAL CASES:
//cache:
int _a = a;
int _b = b;
printf("NAIVE_MAX(%d++, %d++) => %d\n", _a, _b, NAIVE_MAX(a++, b++));
//reset:
a = _a;
b = _b;
printf("NAIVE_MIN(%d++, %d++) => %d\n", _a, _b, NAIVE_MIN(a++, b++));
//reset:
a = _a;
b = _b;
printf("NAIVE_MAX(++%d, ++%d) => %d\n", _a, _b, NAIVE_MAX(++a, ++b));
//reset:
a = _a;
b = _b;
printf("NAIVE_MIN(++%d, ++%d) => %d\n", _a, _b, NAIVE_MIN(++a, ++b));
printf("\nOuch, this doesn't look right at all!\n\n");
#pragma mark NON-FATAL CASES:
//reset:
a = _a;
b = _b;
printf("MAX(%d++, %d++) => %d\n", _a, _b, MAX(a++, b++));
//reset:
a = _a;
b = _b;
printf("MIN(%d++, %d++) => %d\n", _a, _b, MIN(a++, b++));
//reset:
a = _a;
b = _b;
printf("MAX(++%d, ++%d) => %d\n", _a, _b, MAX(++a, ++b));
//reset:
a = _a;
b = _b;
printf("MIN(++%d, ++%d) => %d\n", _a, _b, MIN(++a, ++b));
printf("\nAh, much better now.\n\n");
return 0;
}
Console log:
控制台日志:
NAIVE_MAX(3, 5) => 5
NAIVE_MIN(3, 5) => 3
MAX(3, 5) => 5
MIN(3, 5) => 3
Everything fine so far...
NAIVE_MAX(3++, 5++) => 6
NAIVE_MIN(3++, 5++) => 4
NAIVE_MAX(++3, ++5) => 7
NAIVE_MIN(++3, ++5) => 5
Ouch, this doesn't look right at all!
MAX(3++, 5++) => 5
MIN(3++, 5++) => 3
MAX(++3, ++5) => 6
MIN(++3, ++5) => 4
Ah, much better now.
So never ever use the naive implementationas seen in the code above (and as suggested by Brandon Bodnár, sorry buddy ;) ) if you want to avoid worst cases like these.
因此,如果您想避免像这样的最坏情况,请永远不要使用上面代码中看到的幼稚实现(正如 Brandon Bodnár 所建议的那样,抱歉,伙计 ;))。
回答by Kevin Reid
This is probably not a good idea for this particular application, but it is possibleto write Objective-C methods with parameters “without names”, or rather with zero-length names:
对于这个特定的应用程序,这可能不是一个好主意,但是可以编写带有“无名称”参数的 Objective-C 方法,或者更确切地说是零长度的名称:
+ min:(int)a :(int)b;
...
[Utils min:a :b]
(The selector would be @selector(min::).)
(选择器是@selector(min::)。)
回答by Daniel Yankowsky
Objective-C class methods use named parameters, period. That's just the way it is.
Objective-C 类方法使用命名参数,句点。就是那样子。
Why not make it a global, free function? You shouldn't need a Utils class for this kind of thing.
为什么不让它成为一个全球性的、免费的功能呢?对于这种事情,您不应该需要 Utils 类。
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.
如果不想弄乱全局命名空间,可以使用 Objective-C++(将所有 .m 文件重命名为 .mm)并将其放入命名空间。
回答by TJez
In a template file named "XXIntegerMath.h" drop this...
在名为“XXIntegerMath.h”的模板文件中删除这个...
#import <Foundation/Foundation.h>
static inline NSInteger imax(NSInteger a, NSInteger b) {
return a > b ? a : b;
}
static inline NSInteger imin(NSInteger a, NSInteger b) {
return a < b ? a : b;
}
Then in your objective-c class ...
然后在你的 Objective-c 课上......
#import "XXIntegerMath.h"
NSInteger minValue = imin(someValue, someOtherValue);
It doesn't suffer from the problems described by Regexident.
它不会遇到 Regexident 描述的问题。
回答by Albert Renshaw
Here is a macro I created for multi-max and multi-min which allows more than just 2 inputs.
这是我为 multi-max 和 multi-min 创建的宏,它允许超过 2 个输入。
float a = MMAX(1,2,9.33,2.5); //a = 9.33
float a = MMAX(1,2,9.33,2.5); //a = 9.33
The internal mechanisms use long double and you'll just cast the output to whatever variable you're using. I'd prefer a solution using typeof but couldn't figure out how to do it on __VA_ARGS__on a per argument basis, maybe someone more versed than me in C can figure it out and comment? Anyways, here's the macro definition:
内部机制使用 long double ,您只需将输出转换为您正在使用的任何变量。我更喜欢使用 typeof 的解决方案,但无法__VA_ARGS__在每个参数的基础上弄清楚如何做到这一点,也许比我更精通 C 的人可以弄清楚并发表评论?无论如何,这是宏定义:
#define MMAX(...) ({\
long double __inputs[(sizeof((long double[]){__VA_ARGS__})/sizeof(long double))] = {__VA_ARGS__};\
long double __maxValue = __inputs[0];\
for (int __i = 0; __i < (sizeof((long double[]){__VA_ARGS__})/sizeof(long double)); ++__i) {\
long double __inputValue = __inputs[__i];\
__maxValue = __maxValue>__inputValue?__maxValue:__inputValue;\
}\
__maxValue;\
})
#define MMIN(...) ({\
long double __inputs[(sizeof((long double[]){__VA_ARGS__})/sizeof(long double))] = {__VA_ARGS__};\
long double __minValue = __inputs[0];\
for (int __i = 0; __i < (sizeof((long double[]){__VA_ARGS__})/sizeof(long double)); ++__i) {\
long double __inputValue = __inputs[__i];\
__minValue = __minValue<__inputValue?__minValue:__inputValue;\
}\
__minValue;\
})

