仍在学习基础知识。我有一个函数,其中有一个 block ,需要重复而不再次调用整个函数。这在 Swift 中是如何完成的?
func connected(to peripheral: Peripheral) {
let cwConnection = CWStatusBarNotification()
cwConnection.display(withMessage: "Ring Connected", forDuration: 3)
BluejayManager.shared.getHeliosInfo { (success) in
if success {
// Go on
} else {
// Repeat this block (BluejayManager.shared.getHeliosInfo)
}
}
}
最佳答案
嘿 Riyan 这很简单。这是您的问题的解决方案。只需将 block 放在其他小方法中,当您只需要调用该 block 时调用该小函数即可。
func connected(to peripheral: Peripheral) {
let cwConnection = CWStatusBarNotification()
cwConnection.display(withMessage: "Ring Connected", forDuration: 3)
self.callBluejayManagerShared() // Call of block from method
}
func callBluejayManagerShared(){
BluejayManager.shared.getHeliosInfo { (success) in
if success {
// Go on
} else {
// Repeat this block (BluejayManager.shared.getHeliosInfo)
self.callBluejayManagerShared()
}
}
}
现在,当您只想调用 block 时,只需调用 self.callBluejayManagerShared()
方法即可。
希望对你有帮助
关于ios - 如何重复函数 Swift 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47333167/