【发布时间】:2014-06-13 14:40:54
【问题描述】:
目前,我将一个闭包作为一个对象的属性传递,该对象不接受任何参数且没有返回值,如下所示:
class MyClass {
var myClosureProperty: (() -> ())? {
didSet {
doSomeActionWhenClosureIsSet()
}
}
}
var instanceOfMyClass = MyClass()
instanceOfMyClass.myClosureProperty = {
// do some things here...
}
到目前为止,效果很好。在设置此闭包以在 MyClass 的实例中使用时,我希望能够传入一个参数。我正在寻找类似下面的东西,尽管我确定语法不正确:
class MyClass {
var myClosureProperty: ((newString: String) -> ())? {
didSet {
doSomeActionWhenClosureIsSet(newString)
}
}
func doSomeActionWhenClosureIsSet(stringParam: String) -> () {
// create a button with the stringParam title...
}
}
var instanceOfMyClass = MyClass()
instanceOfMyClass.myClosureProperty = {("Action")
exampleFunction()
}
我将如何向这个闭包传递一个可以在 MyClass 内部使用的参数 - 即一个可以在属性本身的 didSet 部分中使用的值,如第二个示例所示?
编辑:这就是最终对我有用的东西:
class MyClass {
var myClosurePropertyWithStringAndAction: (buttonName: String, closure: (() -> ()))? {
didSet {
let buttonTitle = myClosurePropertyWithStringAndAction!.buttonName
createButtonWithButtonTitle(buttonTitle)
}
}
func createButtonWithButtonTitle(buttonTitle: String) -> () {
// here I create a button with the buttonTitle as the title and set
// handleButtonPressed as the action on the button
}
func handleButtonPressed() {
self.myClosurePropertyWithStringAndAction?.closure()
}
}
}
这是我在实例上的调用方式:
instaceOfMyClass.myClosurePropertyWithStringAndAction = ("Done", {
// do whatever I need to here
})
【问题讨论】:
-
您是否只是在寻找使用输入参数定义闭包的语法?你的问题有点不清楚
-
抱歉,在编辑时按退格键,我返回了一个页面,当我回来时,一些更改不存在。让我用实际问题编辑我的帖子。
-
我认为您正在尝试做的事情存在一些逻辑问题...
newString是闭包的输入参数的名称。设置闭包时,该值为 unknown。您的didSet无法访问它,因为当您调用闭包并实际执行它时,该值将被传入。 -
同样,当您设置
myClosureProperty时,将SUCCESS传递给闭包是没有意义的,因为您没有调用它,您只是在定义关闭。 -
这就是我正在尝试做的事情,也许它会更有意义,也许我选择的带有“成功”的字符串令人困惑......我想将一个字符串和一个动作传递给 MyClass。当我在 MyClass 的实例上设置闭包属性时,我想调用一个函数,该函数使用该字符串作为标题创建一个按钮,并且当按下按钮时,我希望运行闭包中定义的操作。在上面的示例中,当 MyClass 出现在屏幕上(一个 UIView)时,我希望有一个按钮显示“Action”,并且当按下该按钮时调用函数 exampleFunction()。