Use of "#" in Swift

Source: Internet
Author: User

    • Configure external parameter names

Add "#" to the parameter name of the function (or method) before it can have the same local parameter name and external parameter name.

Note: In the method, the second and subsequent parameters, the default is with the internal parameters consistent with the external parameter name, only the first parameter is not, you can use the "#" symbol to force the first parameter to the local parameter name consistent with the external parameter name, but is not recommended.

(1) function
Func IncrementBy (Amount:int, #numberOfTimes: Int) {
Count + = Amount * Numberoftimes
}
Numberoftimes local parameter names (which can only be used inside functions) and external parameter names (used as tags when calling functions) are numberoftimes.

(2) method
Class Counter {
var count:int = 0
Func IncrementBy (#amount: Int, Numberoftimes:int) {
Count + = Amount * Numberoftimes
}
}
Numberoftimes default has local parameter name and external parameter name, amount does not have external parameter name, use "#" to force specify external parameter name for amount, when calling the method, the external parameter name must be used, otherwise there will be a compilation error, prompting missing parameter label.
var counter = counter ()
Counter.incrementby (Amount:4, Numberoftimes:5)
println (Counter.count)//The result is 20

Use of "#" in Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.