与MIN和MAX宏相同的Swift [英] Swift equivalent for MIN and MAX macros

查看:106
本文介绍了与MIN和MAX宏相同的Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C / Objective-C中,使用MIN和MAX宏可以找到两个数字之间的最小值和最大值。 Swift不支持宏,并且在语言/基础库中似乎没有等价物。是否应该使用自定义解决方案,可能是基于这种的泛型? p>

解决方案

min max

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

请参阅记录&在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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