如何重复部分功能 Swift
How repeat part of function Swift
仍在学习基础知识。我有一个函数,其中有一个块,需要重复而不再次调用整个函数。 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 这很简单。这是您的问题的解决方案。只需将块放在其他小方法中,当您只需要调用该块时调用该小函数即可。
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()
}
}
}
现在,当您只想调用块时,只需调用 self.callBluejayManagerShared()
方法即可。
希望这对你有帮助
您可以使用 repeat - while
并 BluejayManager.shared.getHeliosInfo
检查 success
作为中断条件:
repeatGetInfo: repeat {
BluejayManager.shared.getHeliosInfo
{ (success) in
if success
{
// do your stuff.
break repeatGetInfo
} else
{
continue repeatGetInfo
}
}
} while true
希望对您有所帮助
仍在学习基础知识。我有一个函数,其中有一个块,需要重复而不再次调用整个函数。 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 这很简单。这是您的问题的解决方案。只需将块放在其他小方法中,当您只需要调用该块时调用该小函数即可。
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()
}
}
}
现在,当您只想调用块时,只需调用 self.callBluejayManagerShared()
方法即可。
希望这对你有帮助
您可以使用 repeat - while
并 BluejayManager.shared.getHeliosInfo
检查 success
作为中断条件:
repeatGetInfo: repeat {
BluejayManager.shared.getHeliosInfo
{ (success) in
if success
{
// do your stuff.
break repeatGetInfo
} else
{
continue repeatGetInfo
}
}
} while true
希望对您有所帮助