与MIN和MAX宏相同的Swift

编程入门 行业动态 更新时间:2024-10-22 08:01:11
本文介绍了与MIN和MAX宏相同的Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在C / Objective-C中,使用MIN和MAX宏可以找到两个数字之间的最小值和最大值。 Swift不支持宏,并且在语言/基础库中似乎没有等价物。是否应该使用自定义解决方案,可能是基于这种一的泛型? p>

解决方案

min 和 max

func max 请参阅记录&在Swift中没有记录的内置函数。

In C / Objective-C it is possible to find the minimum and maximum value between two numbers using MIN and MAX macros. Swift doesn't support macros and it seems that there are no equivalents in the language / base library. Should one go with a custom solution, maybe based on generics like this one?

解决方案

min and max are already defined in Swift:

func max<T : Comparable>(x: T, y: T, rest: T...) -> T func min<T : Comparable>(x: T, y: T, rest: T...) -> T

See this great writeup on documented & undocumented built-in functions in Swift.

更多推荐

与MIN和MAX宏相同的Swift

本文发布于:2023-10-27 11:23:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MIN   MAX   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!