我正在尝试用这段代码做一个非常简单的弹出到 Root View Controller 的延迟:
let delay = 1 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
self.navigationController?.popToRootViewControllerAnimated(true)
})
但我收到错误 '[AnyObject]?'不是 'Void' 的子类型
我假设它与在 block /闭包内调用 self.navigationController
有关,因为如果我注释掉该行并替换为 println("Will this compile")
它有效。
有人可以解释为什么我会收到此错误以及实现我想要做的事情的正确方法是什么吗?
Xcode 6.1.1
谢谢。
最佳答案
在 swift 单语句闭包中自动返回语句返回值。在您的特定情况下,它试图返回 [AnyObject]?
的实例,这是 popToRootViewControllerAnimated
的返回值。 dispatch_after
期望的闭包是 Void -> Void
。由于闭包返回类型不匹配,编译器会对此进行提示。
要解决这个问题,只需添加一个明确的 return 语句:
dispatch_after(time, dispatch_get_main_queue(), {
self.navigationController?.popToRootViewControllerAnimated(true)
return
^^^
})
关于ios - dispatch_after 错误 : '[AnyObject]?' is not a subtype of 'Void' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27314885/