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

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

问题描述

我要定义一个类utils的一个最大和最小的方法。

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的分(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实现,你可能无法访问到predefined 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.

这篇关于你如何定义一个简单的&QUOT;分&QUOT;方法OBJ-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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