传递函数和inout参数

时间:2014-12-30 03:21:30

标签: swift

构造函数正在接收两个参数。

a)一个函数,其类型是接收inout参数的函数(因此它可以改变参数)

b)传递你的params对象,期望传递的函数能够修改对象

然后触发apply方法,这样你传递的函数就可以改变传递的参数。

如果有一个解决方案,在封闭中传递参数,那也可以。

class Test {

    var params: Any?

    var myFunc: (inout params: Any?) -> () = { (inout params: Any?) in return } //default value


    //constructor receving a function and a address to inout params object
    init(myFunc: (inout params: Any?) -> (), inout params: Any?) {
        self.myFunc = myFunc
        self.params = params
    }

    //call the passed function (extern) and pass the address of given params
    func apply() {
        self.myFunc(params: &self.params)
    }
}

func extern(inout params: Any?) {
    var a = params as [String: Int]
    a["factor"] = 11
}

var p: Any = ["factor": 10]
var test = Test(myFunc: extern, params: &p)

test.apply() //p["factor"] should be 11 now here

p["factor"] as [String: Int]

我第二次尝试使用闭包

//Utility

class Test {

    var closure: () -> Any

    var myFunc: (message: String, closure: () -> Any) -> () = { (message: String, closure: () -> Any) in return }

    init(myFunc: (message: String, closure: () -> Any) -> (), closure: () -> Any) {
        self.myFunc = myFunc
        self.closure = closure
    }

    func apply(message: String) {
        self.myFunc(message: message, closure: self.closure)
    }

}

//users of the utility

func extern(message: String, closure: () -> Any) {
    println(message)
    var name = closure() as [String: String]
    name["name"] = "Doe"
}

var name: Any = ["name": "John"]

var test = Test(myFunc: extern, closure: {name})
test.apply("hello ")
name //it's still John here

使用AnyObject和闭包的第三次尝试,当然它可以工作,但仍然需要你的意见家伙的最佳策略。

//Utility

class Test {

    var closure: () -> AnyObject

    var myFunc: (message: String, closure: () -> AnyObject) -> () = { (message: String, closure: () -> AnyObject) in return }

    init(myFunc: (message: String, closure: () -> AnyObject) -> (), closure: () -> AnyObject) {
        self.myFunc = myFunc
        self.closure = closure
    }

    func apply(message: String) {
        self.myFunc(message: message, closure: self.closure)
    }

}

//users of the utility

func extern(message: String, closure: () -> AnyObject) {
    println(message)
    var name: Name = closure() as Name
    name.name = "Doe"
}

class Name {
    var name = "John"
}

var name = Name()

var test = Test(myFunc: extern, closure: {name})
test.apply("hello ")
name //it's changed here

1 个答案:

答案 0 :(得分:1)

看起来你正试图在这里重新发明封闭。闭包的一个方面是它们可以捕获对它们周围值的引用。这应该按照您上面描述的内容进行操作,并让您继续为变量使用正确的类型,而不是依赖于Any

class Test {
    var myFunc: () -> Void = { }

    init(myFunc: () -> Void) {
        self.myFunc = myFunc
    }

    func apply() {
        self.myFunc()
    }
}

func extern(inout dict: [String: Int]) {
    dict["factor"] = 11
}

var p = ["factor": 10]

let test = Test {
    extern(&p)
}

test.apply() // p["factor"] is now 11 here

println(p)
// ["factor": 11]