是否可以在词典中存储闭包(我们如何在词典中存储ObjC块)?例如:
data = [String:AnyObject]()
data!["so:c0.onSelection"] = {() in
Debug.log(.Debug, message: "Hello, World!")
}
答案 0 :(得分:18)
你可以,但有一些限制。首先,函数类型不从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!")
}
答案 1 :(得分:4)
我有不同的方法
我创建了一个"持有者"类可以保持你的闭包:
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)
答案 2 :(得分: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能够在未来实现这一目标。
干杯!
答案 3 :(得分:0)
这个简单的例子帮助我理解了一点:
//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!"