斯威夫特 - 带参数的回调函数Anonymus(语法) [英] Swift - Anonymus function with arguments as callback (syntax)

查看:109
本文介绍了斯威夫特 - 带参数的回调函数Anonymus(语法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我潜入斯威夫特郎苹果,并有一些probles与调用带参数的简单的匿名函数作为另一个函数的参数,例如:

I'm diving into Swift lang by Apple and have some probles with calling simple anonymous function with arguments as an argument of another function, example:

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

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

如何解决?

推荐答案

你打错闭包语法

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

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

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

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

这篇关于斯威夫特 - 带参数的回调函数Anonymus(语法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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