我正在尝试编写一个函数来检查我是否已达到64个本地通知的限制 . 有一些答案可以处理UIlocalNotifications,但我还没有找到一个NSlocalNotifications . 这是我的功能
func notificationLimitreached() {
let center = UNUserNotificationCenter.current()
var limit = false
center.getPendingNotificationRequests(completionHandler: { requests in
print(requests.count)
if requests.count > 59 {
limit = true
print(limit)
} else {
limit = false
}
})
print (limit)
问题是“limit”变量在闭包内部时打印为true,然后在离开闭包后重置为初始值false .
我尝试过的其他东西 .
- 当我读取此值时,再次在内部闭包时设置全局变量,否则将其设置为其原始值
1 回答
正如您所看到的,您正面临着异步逻辑:
您的函数首先打印为false,因为getPendingNotificationRequests闭包中存在延迟 .
试试这个功能,看看是否有效:
您可以使用以下代码调用此函数: