为什么在case 1
中,我的函数返回的是["reached end": "reached end"]
而不是响应字典的值?如何使其返回值为responseDictionary
?
background:HelperClass.doMath
接受一个数字并具有一个返回字典的完成块
func handleTask(task: [String: AnyObject]) -> AnyObject {
switch task {
case 1:
if let bigNumber = task[bigNumberKey] as? NSNumber {
HelperClass.doMath(bigNumber, replyBlock: {(responseDictionary: [NSObject: AnyObject]!) -> Void in
return [responseDictionary]
})
}
case 2:
return 2
case 3:
return 3
default:
break
}
return ["reached end": "reached end"]
}
这应该有效:
func handleTask(task: [String: AnyObject]) -> AnyObject {
switch task {
case 1:
if let bigNumber = task[bigNumberKey] as? NSNumber {
return HelperClass.doMath(bigNumber, replyBlock: {(responseDictionary: [NSObject: AnyObject]!) -> Void in
return [responseDictionary]
})
}
case 2:
return 2
case 3:
return 3
default:
break
}
return ["reached end": "reached end"]
}
replyBlock
的doMath
方法,并在那里用于某些目的。它在您的HelperClass
函数范围内执行。因此,handleTask
将您带回到您的return [responseDictionary]
范围,并且不会导致case 1
返回任何内容。handleTask
的结果。
关于swift - 从闭包中的函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31840621/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |