您如何定义一个简单的“分钟”? obj-c 中的方法
我想在 Utils 类中定义 min 和 max 方法。
@interface Utils
int min(int a, int b);
int max(int a, int b);
@end
但我不想有命名参数。这将是一个太沉重的符号。我想使用 C 风格的定义。但是,[Utils min(a, b)]
作为调用不起作用。我的问题是什么?
预先感谢您的任何帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它已经被定义为宏。
您不需要重新定义这些。
It is already defined as a macro.
You dont need to redefine these ones.
Brandon Bodnár 发布的解决方案存在严重问题(在撰写本文时该解决方案已被标记为有效解决方案)。
此处描述的问题: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
自己检查一下:
控制台日志:
所以永远不要使用天真的实现,如所示如果您想避免像这样的最坏情况,请使用上面的代码(按照 Brandon Bodnár 的建议,对不起,伙计;))。
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).
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.
由于您没有使用 Objective-C 的 OS X 实现,因此您可能无法访问预定义的 MIN 和 MAX 宏。
您可以自己定义它们,因为
可能有更好的方法来定义它们,但这些将创建简单的宏供您使用。您可以将它们添加到您的类通常共享的任何通用 .h 文件中。
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 方法,或者更确切地说带有零长度名称:(
选择器将是 <代码>@selector(min::)。)
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::)
.)Objective-C 类方法使用命名参数,句点。事情就是这样。
为什么不让它成为一个全球性的、免费的功能呢?对于这种事情,你不应该需要 Utils 类。
如果您不想让全局命名空间变得混乱,可以使用 Objective-C++(将所有 .m 文件重命名为 .mm)并将其放入命名空间中。
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.
在名为“XXIntegerMath.h”的模板文件中,将其删除...
然后在您的 Objective-C 类中...
它不会遇到 Regexident 描述的问题。
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.
这是我为 multi-max 和 multi-min 创建的宏,它允许超过 2 个输入。
float a = MMAX(1,2,9.33,2.5); //a = 9.33
内部机制使用 long double,您只需将输出转换为您正在使用的任何变量。我更喜欢使用 typeof 的解决方案,但无法弄清楚如何在每个参数的基础上在 __VA_ARGS__ 上执行此操作,也许比我更精通 C 的人可以弄清楚并发表评论?无论如何,这是宏定义:
Here is a macro I created for multi-max and multi-min which allows more than just 2 inputs.
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: