久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Type 應該采用什么協議來讓泛型函數將任何數字類

What protocol should be adopted by a Type for a generic function to take any number type as an argument in Swift?(Type 應該采用什么協議來讓泛型函數將任何數字類型作為 Swift 中的參數?) - IT屋-程序員軟件開發技術
本文介紹了Type 應該采用什么協議來讓泛型函數將任何數字類型作為 Swift 中的參數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想讓一個函數在 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Which is the best way to estimate measure of photographed things?(哪種方法是估計拍攝物體尺寸的最佳方法?)
Convert Character to Int in Swift 2.0(在 Swift 2.0 中將字符轉換為 Int)
Edit Text shows exception Invalid Int quot;quot;(編輯文本顯示異常 Invalid Int “;)
How to convert an Int to a Character in Swift(如何在 Swift 中將 Int 轉換為字符)
In Swift 3, how to calculate the factorial when the result becomes too high?(在 Swift 3 中,當結果變得太高時如何計算階乘?)
How to create a hex color string UIColor initializer in Swift?(如何在 Swift 中創建十六進制顏色字符串 UIColor 初始化程序?)
主站蜘蛛池模板: 91精品一区 | 一区二区三区高清 | 夜夜爽99久久国产综合精品女不卡 | 国产美女在线观看 | 亚洲一区| 在线中文字幕亚洲 | 欧美不卡一区二区 | 久久国产精品一区二区三区 | 婷婷在线免费 | pacopacomama在线 | 成人免费一级视频 | 69电影网 | 午夜精品久久久久久久99黑人 | 中文字幕精品一区 | 91高清在线观看 | 91久久精品国产免费一区 | 精品av| 国产97在线 | 日韩 | 日韩成人免费视频 | 国产999精品久久久 午夜天堂精品久久久久 | 成人国产精品色哟哟 | 日本久久一区二区三区 | 国产91丝袜在线熟 | 天天人人精品 | av永久 | 成人av观看| 亚洲国产成人精品女人久久久 | 国内精品免费久久久久软件老师 | 欧美精品在欧美一区二区少妇 | 懂色av蜜桃av | 亚洲成人三级 | 亚州综合在线 | 91在线免费视频 | 91久久夜色精品国产网站 | 成人在线免费观看 | 久久综合久久综合久久综合 | 精品国产一区二区三区成人影院 | 亚洲视频一区 | 一区二区三区成人 | 国产精品久久久久一区二区三区 | 日本三级网站在线 |