Local and external parameter names for the Swift method

Source: Internet
Author: User
first, what is the name of the external parameter.

To put it simply, an external parameter name is to add a specific name to the method's arguments before it is invoked, in order to facilitate reading code and improve maintenance efficiency.

in the latest Xcode, the properties and usages of the external parameter names are as follows Nature:

A function can have both a local name (used internally) and an external name (used when calling a function), and the method's arguments are similar, although the default behavior of the method and function's local name and external name is not the same

The default for Swift is to give the first parameter name a local parameter name, and the second and subsequent parameter local parameter names and external parameter names default to the same usage example:

In other languages, if we define a method in this way:

We generally call this:

And there's a mistake in Swift's wording,

As defined above, the system has the following error:

You should call this:

The so-called argument labels is the name of the external parameter.

I was overwhelmed when I first touched this thing because I didn't show the external parameter name, but the system added the default name of the external parameter that is the same as the formal parameter. But the strange thing is that the first parameter x does not default to the name of the external parameter. The reason is:

The default for Swift is to give the first parameter name a local parameter name, and the second and subsequent parameter local parameter names and external parameter names are the same by default.

To modify the external parameter name of a method

Sometimes it is useful to provide an external parameter name for the first argument of the method, which can be displayed before the parameter by adding a # number
If you do not want to provide an external parameter name for the second and subsequent parameters of the method, use the underscore (_) as the explicit external name for the parameter.

1, for example, you want to call the method like this:

You should define a method like this:

2, and for example, you want to call the method like this:

You should define a method like this:

3, or show other external parameter names

This definition:

This call:

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.