問題描述
我想讓一個函數在 Swift 中接受任何數字(Int、Float、Double、...)
I want to make a function accept any number (Int, Float, Double, ...) in Swift
func myFunction <T : "What to put here"> (number : T) -> {
//...
}
不使用 NSNumber
without using NSNumber
推薦答案
更新: 下面的答案原則上仍然適用,但是 Swift 4 完成了對數字協議的重新設計,以便添加您自己的通常是不必要的.在構建自己的系統之前,請查看標準庫的數字協議.
Update: The answer below still applies in principle, but Swift 4 completed a redesign of the numeric protocols, such that adding your own is often unnecessary. Take a look at the standard library's numeric protocols before you build your own system.
這實際上在 Swift 中是不可能開箱即用的.為此,您需要創建一個新協議,使用您將在泛型函數中使用的任何方法和運算符進行聲明.此過程對您有用,但具體細節將在一定程度上取決于您的通用函數的作用.以下是獲取數字 n
并返回 (n - 1)^2
的函數的方法.
This actually isn't possible out of the box in Swift. To do this you'll need to create a new protocol, declared with whatever methods and operators you're going to use inside your generic function. This process will work for you, but the exact details will depend a little on what your generic function does. Here's how you'd do it for a function that gets a number n
and returns (n - 1)^2
.
首先,定義您的協議,使用運算符和一個采用 Int
的初始化程序(這樣我們就可以減去一個).
First, define your protocol, with the operators and an initializer that takes an Int
(that's so we can subtract one).
protocol NumericType {
func +(lhs: Self, rhs: Self) -> Self
func -(lhs: Self, rhs: Self) -> Self
func *(lhs: Self, rhs: Self) -> Self
func /(lhs: Self, rhs: Self) -> Self
func %(lhs: Self, rhs: Self) -> Self
init(_ v: Int)
}
所有數字類型已經實現了這些,但此時編譯器并不知道它們是否符合新的NumericType
協議.你必須明確這一點——Apple 稱之為通過擴展聲明采用協議".我們將為 Double
、Float
和所有整數類型執行此操作:
All of the numeric types already implement these, but at this point the compiler doesn't know that they conform to the new NumericType
protocol. You have to make this explicit -- Apple calls this "declaring protocol adoption with an extension." We'll do this for Double
, Float
, and all the integer types:
extension Double : NumericType { }
extension Float : NumericType { }
extension Int : NumericType { }
extension Int8 : NumericType { }
extension Int16 : NumericType { }
extension Int32 : NumericType { }
extension Int64 : NumericType { }
extension UInt : NumericType { }
extension UInt8 : NumericType { }
extension UInt16 : NumericType { }
extension UInt32 : NumericType { }
extension UInt64 : NumericType { }
現在我們可以編寫我們的實際函數,使用 NumericType
協議作為通用約束.
Now we can write our actual function, using the NumericType
protocol as a generic constraint.
func minusOneSquared<T : NumericType> (number : T) -> T {
let minusOne = number - T(1)
return minusOne * minusOne
}
minusOneSquared(5) // 16
minusOneSquared(2.3) // 1.69
minusOneSquared(2 as UInt64) // 1
這篇關于Type 應該采用什么協議來讓泛型函數將任何數字類型作為 Swift 中的參數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!