我需要将 UI 与模块连接起来。
错误:无法将类型“()”的值转换为闭包结果类型“Bool”
Controller 代码:
private func connectLogicView() {
screenView.handlePlayPauseButton(sender: screenView.playPauseButton) { () -> Bool in
obj.playPause(queue: .global()) { //Error here
if self.obj.getPlayer().timeControlStatus != .paused {
return true
} else if self.obj.getPlayer().timeControlStatus == .paused {
return false
}
}
}
}
模块代码:
public func playPause(queue: DispatchQueue, completion: @escaping ()->Void){
if player.timeControlStatus == .playing {
player.pause()
} else {
player.play()
}
queue.async {
completion()
}
}
用户界面代码:
func handlePlayPauseButton(sender: UIButton, completion: () -> Bool) {
if completion() == true {
sender.setTitle("pause", for: .normal)
} else {
sender.setTitle("play", for: .normal)
}
}
如果您能提供更好的方法,我们将不胜感激。
最佳答案
playPause
方法在 completion
block 中没有任何内容,但是
obj.playPause(queue: .global()) {
if self.obj.getPlayer().timeControlStatus != .paused {
return true // NO NEED FOR THIS LINE
} else if self.obj.getPlayer().timeControlStatus == .paused {
return false // NO NEED FOR THIS LINE
}
}
您返回 true 或 false 删除它,您的代码将正常运行
关于swift - 将 UI 与逻辑 Swift 连接时出现关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854136/