The Swift function call does not write the parameter name

Source: Internet
Author: User

Recently we really started to learn Swift, and when we called the function, we had a problem: do we write the function name?

Let's take a look at two examples:

1FuncTest(A:int, B:int)Int {Return a + b}test (a:1, B:1)(A) Test (1, B:1)B2ClassTest {var name:Stringvar Age:IntInit (Name:string, Age: int) {self.name = name self.age = age} func sayhello (word:string, place:string) {println ( Span class= "hljs-string" > "Hello \ (self.name), \ (word) at \ (place)"}}var test = test (" Jack ", Age: 12) / /(C) Test.sayhello (Word:  "nice to meet You", place:  "Beijing") Span class= "Hljs-comment" >//(D)            

(A),,, (B) (C) (D) call everywhere, which will be an error?

Please
Zi
Fine
Think
Test
One
Under

Or
Stakeholders
Play
Open
Playground
Transport
Yes
One
Under

Well, if you go straight over here, there's nothing I can do about it.

The answer is: all the errors everywhere.

The correct wording is:

test(1, 1)var test = Test(name: "Jack", age: 12)test.sayHello("nice to meet you", place: "Beijing")

Is the foot numb? Hemp is right, I stamp I also hemp.


My IQ basically said goodbye to Swift.

First, let's be clear that there are three calls in Swift:

    • function calls (closures are also attributed to functions, although all functions are essentially closures.) This sentence can not understand the automatic skip, just to prevent people literal kind)
    • Class initialization
    • Method invocation

If there are no parameters, then it () is called directly, so the following premise is the need to pass the parameter, and the number of arguments is greater than one.

The example in the previous section is the typical three invocations, which are correctly spelled when you pass the arguments:

<函数名>(参数值,参数值...) // 不加任何参数名,直接写参数值<实例>.<方法名>(参数值,参数名:参数值,参数名:参数值...) // 方法调用第一个参数不写参数名,后面的全部要写。特殊情况是尾闭包,往下看<类初始化>(参数名:参数值,参数名:参数值...) // 类初始化所有参数都需要加参数名

The invocation of a single function is well understood and is mostly done in other languages. We mainly explain both the method invocation and the class initialization of these two calls.

Why does Swift have such a strange limitation on method invocation and class initialization parameter names? The main reason is the consistent tradition of inheriting objective-c. Let's look at the notation in OC:

 [person setName:@"sam" andSecondName:@"job"]

setNameIs the method name followed by the first argument, which corresponds to the wording in Swift:

person.setName("sam", andSecondName: "job")

That is, the name of the first parameter is already implied in the method name (although we do not know what the first parameter name is, but obviously the first parameter is Name , we can know that the first argument is the name), so omit the first parameter name.

So init why add the first parameter name?

Look directly at the code:

initWith:"Sam", andSecondName: "job"] // ocTest(name: "Sam", andSecondName: "job") / swift

Because the class name is used directly when initializing in Swift, there is no method name, so the first parameter name cannot be omitted.

Special cases

Here are a few special cases.

Tail Closure Package

The first is the tail closure package.

The last parameter to many of the methods in Swift is handler that we can pass in a closure. As closures are cumbersome to write to the parameter list, Swift provides a new way of writing: the tail closure package. See Example:

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {  ......})

UIAlertActionThe last argument is handler that it is written here with a trailing closure, which is to add the closure directly behind the closing parenthesis. Of course, you can also write the closure to the parameter list, just need to add parameter names.

If the function requires only handler One argument, you can omit the parentheses for the method call:

aaa.sort {  ...}
Default value

Parameters can write default values, but default values have many rules:

    • If the default value is used, the parameter corresponding to the default value must be written with the argument name when the call is called. The main effect here is function and method invocation, because class initialization would have been to write the full parameter name.
    • If the default value is used and the default value does not appear at the end, then all parameters must be written at the time of the call.

Combined with the above two points, we recommend that you use the default values, the parameters with the default values at the end of the list, this is much easier.

Force specified parameter name

If you want to force a call to require a parameter name, you can add an external parameter name to the argument at the time of the declaration:

func test(outName name: String, outAge age: Int) {  ...}test(outName: "asd", outAge: 2)

The corresponding external parameter name must be added to the call.

If the external parameter name is the same as the internal parameter name, you can add it directly before the parameter name # :

func test(#name: String, #age: Int) {  ...}test(outName: "asd", outAge: 2)
Force cancellation of parameter names

For a function that requires a parameter name, you can also force the argument name to be removed before the parameter name _ :

class Test {  func test(name: String, _ age: Int) { ... }}var test = Test()test.test("123", 3)
Anyway

function calls in Swift are really a pit.

The Swift function call does not write the parameter name

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.