Swift development experience-External parameter names

Source: Internet
Author: User

First, what is the external parameter name?

Simply put, the name of an external parameter is called when a method is invoked to precede the parameters of the method with a specific name, in order to facilitate reading code, improve maintenance efficiency.

second, in the latest Xcode, the nature and usage of the external parameter name are as followsProperties:

A function can have both a local name (internal use) and an external name (used when calling a function), and the method's parameters are similar, although the default behavior of the local and external names of methods and functions is different

The first parameter name is given a local parameter name by default in Swift, and the second and subsequent parameter local parameter names and external parameter names are the same by default

Usage examples:

In other languages, if we define a method like this:

We generally call this:

And Swift is wrong in this notation,

As defined above, the system error is as follows:

This should be called:

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

I was at a loss when I first touched this thing, because I didn't show the name of the external parameter, but the system adds the default name of the external argument that is the same as the parameter. But the strange thing is that the first parameter x does not have an external parameter name set by default.

The reasons are:

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

Modify the external parameter name of a method

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

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

The method should be defined like this:

2, for example, you want to call this method:

The method should be defined like this:

3, or display other external parameter names

This defines:

This is called:

Reference page:

http://blog.csdn.net/huangchentao/article/details/32715179

Swift development experience-External parameter names

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.