Swift-尾随闭包语法 [英] Swift - Trailing Closure Syntax

查看:210
本文介绍了Swift-尾随闭包语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Apple的Swift lang,使用尾随闭包语法会遇到一些问题,例如:

I'm diving into Swift lang by Apple and have some problems using the trailing closure syntax, example:

func test(txt: String, resolve: (name: String) -> Void) {
   resolve(name: "Dodo")
}

// Errors here complaining on resolve param
test("hello", (name: String) {
   println("callback")
})

如何解决?

推荐答案

您使用了错误的闭包语法

you have the wrong closure syntax

test("hello", {(name: String) in 
    println("callback")
})

test("hello", {
   println("callback: \($0)")
})

test("hello") {(name: String) in 
    println("callback")
}

test("hello") {
   println("callback: \($0)")
}

这篇关于Swift-尾随闭包语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆