gpt4 book ai didi

ios - 如何在 Swift 中跳过完成处理程序

转载 作者:行者123 更新时间:2023-11-28 13:35:46 24 4
gpt4 key购买 nike

我有一个在完成处理程序中返回 Int 值的函数,但有时,我想在从其他类调用时跳过完成处理程序,只使用 Int 值。下面是我的代码。这里的 totalEvents 是带有完成处理程序的。

就像我需要调用下面的方法

let initialDBCount = self.totalEvents()

func totalEvents(completion: @escaping (_ eventsCount: Int? ) -> Void ) {

self.fetchEvents(forPredicate: nil, withSort: nil, andLimit: nil, completion: { (events) -> Void in
guard let fetchEvents = events else {
return
}
if fetchEvents.count > 0 {
completion(fetchEvents.count)
}
})
}

最佳答案

completion 处理程序设置为可选 并将nil 设置为其默认值,即

func totalEvents(completion: ((_ eventsCount: Int?)->())? = nil)

用法:

totalEvents 两种方式都可以调用,

1. 没有completion handler

totalEvents()

2. 使用completion 处理程序

totalEvents { (value) in
print(value)
}

关于ios - 如何在 Swift 中跳过完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com