Swift - 在字典中存储闭包

15

能否在字典中存储闭包(就像我们可以在 ObjC 中存储块一样)?例如:

   data = [String:AnyObject]()
   data!["so:c0.onSelection"] = {() in
       Debug.log(.Debug, message: "Hello, World!")
   }
4个回答

19

你可以这样做,但有一些限制。首先,函数类型不继承自AnyObject,也没有共同的基类。你可以拥有一个字典[String: () -> Void][String: (String) -> Int],但它们不能存储在同一个字典中。

我还必须使用typealias来定义字典,以便Swift可以正确解析。以下是基于你的片段的示例。

typealias myClosure = () -> Void
var data: [String: myClosure]? = [String: myClosure]()
data!["so:c0.onSelection"] = {() -> Void in
    Debug.log(.Debug, message: "Hello, World!")
}

6

我有一种不同的方法

我创建了一个“holder”类来保存你的闭包,就像这样:

typealias SocialDownloadImageClosure = (image : UIImage?, error: NSError?) -> ()
typealias SocialDownloadInformationClosure = (userInfo : NSDictionary?, error: NSError?) -> ()


private class ClosureHolder
{
 let imageClosure:SocialDownloadImageClosure?
 let infoClosure:SocialDownloadInformationClosure?

 init(infoClosure:SocialDownloadInformationClosure)
 {
    self.infoClosure = infoClosure
 }
 init(imageClosure:SocialDownloadImageClosure)
 {
    self.imageClosure = imageClosure
 }
}

然后我像这样创建了一个字典:
var requests = Dictionary<String,ClosureHolder>()

现在只需这样为字典添加闭包:

self.requests["so:c0.onSelection"] = ClosureHolder(completionHandler)

1

Connor是正确的,我确实尝试了许多方法来将变量和闭包存储在同一个字典中,但失败了并且无法解析出来,Swift反编译器会抛出错误:

"Command failed due to signal: Segmentation fault: 11" (the hell is it?!)

例如:

//This won't work
var params:[String: Any] = ["x":100, "onFoundX": {println("I found X!")}]
if var onFoundX: (()->Void) = params["onFoundX"] as? (()->Void) {
    onFoundX()
}

//This should work by separate into 2 dictionaries and declare the "typealias" obviously
var params:[String: Any] = ["x":"100"}]
var events:[String: (()->Void)] = ["onFoundX": {println("I found X!")]

if var onFoundX: (()->Void) = events["onFoundX"] as? (()->Void) {
    onFoundX() // "I found X!"
}

if var x = events["x"] as? String {
    println(x) // 100
}

我希望Swift将来能够实现这一点。
干杯!

1
这个简单的例子帮助我更好地理解了一些内容:
//Init dictionary with types (i.e. String type for key, Closure type for value):
var myDictionary: [String: ()->(String)] = [:]

//Make a closure that matches the closure signature above and assign to variable (i.e. no parameter and returns a String):
let sayHello: () -> (String) = {
    return "Hello!"
}

//Add closure to dictionary with key:
myDictionary["myFunc"] = sayHello

//Access closure by known key and call it:
myDictionary["myFunc"]!() //"Hello!"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接