这是我遇到的一个奇怪的错误。
我注意到有时会完全忽略return语句。 在这个简单的例子中,"返回结果"虽然执行了其他行,但会被跳过。
func (flag: bool) -> Int?
{
if flag
{
let result = 1 //not skipped
//use result or do other work
return result //skipped, although it most definitely should not be skipped
}
return nil
}
是因为我使用了太多嵌套的闭包吗?在我的应用程序的某些部分,由于并发和错误处理,我有3-5级深度。就像在这个片段中一样。
init?()
{
dispatch_sync(queue){ //level 1
let value = Result(11) { //level 2
if subValue = Result(111){ //level 3
//do work (maybe even on a different queue)
}.then { //level 3
//do more work
}.catch { }
}.then{ //level 2
}
}
}
至少看起来好看3-5嵌套的if-let语句只是为了查看是否发生了错误。
我正在运行Xcode 6.1.1