A case where the method closure parameter in Swift cannot omit parentheses

Source: Internet
Author: User

Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)

We know that in swift, if the last parameter of the method is a closure type, you can omit the argument and follow the function directly behind it with a closure, such as:

func test(count:Int,(Int)->Bool){    //...}

You can call this:

test(11in    //...}

However, there are cases where parentheses cannot be omitted, such as in a For statement:

For dinosaurinchDinosaurs. Filter{(dinosaur), BoolinchDistancebetween (Tower. Spritecomponent. Node, Nodeb:dinosaur. Spritecomponent. Node) < Towertype. Range} {if let T = target{if dinosaur. Spritecomponent. Node. Position. x> t. Spritecomponent. Node. Position. x{target = dinosaur}} else{target = Dinosaur}}

The above statement will give an error, because after the for statement also with {},swift do not know which {}, like tongue twisters ...

You must surround {} with () behind the filter.

However, if you are not in the For statement, you can omit (), for example:

x = dinosaurs.filterin                distanceBetween(tower.spriteComponent.node, nodeB: dinosaur.spriteComponent.node) < towerType.range            }

A case where the method closure parameter in Swift cannot omit parentheses

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.