Some changes of method variable parameter syntax in SWIFT3

Source: Internet
Author: User

We know that in Swift2, the parameter of the method is a let value by default, which is immutable.

But we can add the var keyword before the parameter to change its invariance:

func foo(var i:Int){    i += 1    print(i)}

Unfortunately, in swift2.x+, it is clear that the Modifier keyword var for the method parameter will be removed in Swift3, so in order to produce this hateful warning, you must use the InOut keyword, and add the address character when you call the method to pass the argument:

i:Int){    i += 1    print(i)}i10foo(&i)print(i)

That's all!!!

However, in Xcode8.0beta Swift3.0 in the preview version, the situation has changed, run the above code, the compiler will prompt you inout the keyword misplaced, you have to put the inout behind the colon:

func test( i:inout Int){    i += 1    print(i)}var x = 10test(&x)print(x)

You can combine the one I wrote earlier on how to convert different types of mutable pointers in Swift, I believe you must have something to gain;]

Some changes of method variable parameter syntax in SWIFT3

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.