How do I read this Swift syntax? [duplicate]

五迷三道 提交于 2019-12-02 06:21:40

This is called trailing closure syntax. If the last parameter of a function is a closure, it can be placed in curly braces immediately following a closing parenthesis around the previous parameters.

The get method here takes two parameters: a String and a closure with some signature like (Request) -> ()

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID102

You'll also see cases where the only parameter is a closure, like the map() method on an array. In these situations, the parentheses can be omitted entirely and the closure is written in curly braces immediately following the function name, e.g.:

let lowerCasedWords = arrayOfWords.map{ $0.lowercased() }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!