你如何定义一个简单的“min"?obj-c 中的方法 [英] How would you define a simple "min" method in obj-c

查看:13
本文介绍了你如何定义一个简单的“min"?obj-c 中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Utils 类中定义 min 和 max 方法.

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

但我不想有命名参数.这将是一个过于沉重的符号.我想使用 C 风格的定义.但是随后 [Utils min(a, b)] 作为调用不起作用.我的问题是什么?

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?

在此先感谢您的帮助

推荐答案

由于您没有使用 Objective-c 的 OS X 实现,您可能无法访问预定义的 MIN 和 MAX 宏.

Since you aren't using the OS X Implementation of objective-c, you may not have access to the predefined MIN and MAX macros.

你可以自己定义这些

#define MIN(a,b)    ((a) < (b) ? (a) : (b))
#define MAX(a,b)    ((a) > (b) ? (a) : (b))

可能有更好的方法来定义它们,但这些将创建简单的宏供您使用.您可以将它们添加到您的类通常共享的任何公共 .h 文件中.

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.

这篇关于你如何定义一个简单的“min"?obj-c 中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆