Swift中"#"的用法

來源:互聯網
上載者:User

標籤:round   result   amount   ntb   ber   調用函數   沒有   font   mount   

  • 配置外部參數名

在函數(或者方法)的參數名前添加"#",可以使該參數擁有相同的本地參數名和外部參數名。

註:在方法中,第二個及後續的參數,預設是具有和內部參數一致的外部參數名的,只有第一個參數沒有,可以使用"#"符號來強制給第一個參數添加與本地參數名一致的外部參數名,但是不推薦。

(1)函數
func incrementBy(amount: Int, #numberOfTimes: Int) {
count += amount * numberOfTimes
}
numberOfTimes的本地參數名(只能在函數內部使用)和外部參數名(在調用函數的時候作為標籤使用)都是numberOfTimes。

(2)方法
class Counter {
var count: Int = 0
func incrementBy(#amount: Int, numberOfTimes: Int) {
count += amount * numberOfTimes
}
}
numberOfTimes預設具有本地參數名和外部參數名,amount沒有外部參數名,使用"#"為amount強制指定外部參數名,在調用該方法的時候,必須使用外部參數名,否則會出現編譯錯誤,提示缺少參數標籤。
var counter = Counter()
counter.incrementBy(amount: 4, numberOfTimes: 5)
println(counter.count) // the result is 20

 

Swift中"#"的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.