Swift 在闭包中使用函数

Swift 在闭包中使用函数,swift,Swift,我试图在闭包中使用函数,但收到一个错误“无法将()类型的值转换为闭包结果类型Bool”。下面的代码演示了该错误。我怎样才能做到这一点 func test1(){ test2(){ success in self.test1() } } func test2(completionHandler: (Bool) -> Bool){ completionHandler(true) } 您指定test2闭包返回一个Bool,因此返回一个: func te

我试图在闭包中使用函数,但收到一个错误“无法将()类型的值转换为闭包结果类型Bool”。下面的代码演示了该错误。我怎样才能做到这一点

func test1(){
    test2(){ success in
        self.test1()
    }
}

func test2(completionHandler: (Bool) -> Bool){
    completionHandler(true)
}

您指定
test2
闭包返回一个
Bool
,因此返回一个:

func test1(){
    test2 { (success) -> Bool in
        test1()
        return success
    }
}
如果不想从中返回值,请使用
test2
的closure return void:

func test1(){
    test2 { (success) in
        test1()
    }
}

func test2(completionHandler: (Bool) -> Void){
    completionHandler(true)
}