【发布时间】:2020-07-04 21:34:41
【问题描述】:
我有一个完成返回布尔值的方法。但我不知道如何解决这个错误。
错误
UISwitch.isOn must be used from main thread only
按钮操作
@IBAction func notificationSwitch(_ sender: Any) {
LocalNotification().checkEnabled(completion: { (success) -> Void in
// When download completes,control flow goes here.
if success == false{
print("Cant Turn On")
self.notificationToggle.isOn = false
} else {
print("Can Turn On")
if self.notificationToggle.isOn == true {
self.notificationToggle.isOn = false
} else {
self.notificationToggle.isOn = true
}
}
})
}
也已经尝试将LocalNotifications().... 包装在DispatchQueue.main.async 中,但仍然出现相同的错误
【问题讨论】:
-
你了解“异步”这个词吗?
getNotificationSettings是异步的;你不能从checkEnabled返回任何依赖它的值。请阅读programmingios.net/what-asynchronous-means -
我不能给这个方法添加一个补全吗?我试过了,但我不知道如何调用该方法来检索完成 bool @matt
-
您当然可以添加一个完成处理程序参数。 — 不幸的是,您现在对问题进行了如此彻底的编辑,以至于不再清楚它是关于什么的。我试图回答关于现在的情况。
标签: ios swift uilocalnotification localnotification