gpt4 book ai didi

swift - 如何从系统函数的完成处理程序闭包中返回?

转载 作者:可可西里 更新时间:2023-10-31 23:57:29 31 4
gpt4 key购买 nike

我知道这行不通,因为 completion handlerbackground Thread 上,但是

where am I supposed do dispatch the main queue or what else do i have to do?

这是代码:

static func isNotificationNotDetermined() -> Bool{

var isNotDetermined = false

UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
switch notificationSettings.authorizationStatus {
case .notDetermined:
isNotDetermined = true

case .authorized:
isNotDetermined = false

case .denied:
isNotDetermined = false

}
}

return isNotDetermined
}

最佳答案

你不能这样做; getNotificationSettings 是异步的,因此您应该在方法中传递一个闭包并在切换后立即调用。像这样:

static func isNotificationNotDetermined(completion: (Bool) -> Void) {

UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
var isNotDetermined = false
switch notificationSettings.authorizationStatus {
case .notDetermined:
isNotDetermined = true

case .authorized:
isNotDetermined = false

case .denied:
isNotDetermined = false

}

// call the completion and pass the result as parameter
completion(isNotDetermined)
}

}

然后你将像这样调用这个方法:

    YourClass.isNotificationNotDetermined { isNotDetermined in
// do your stuff
}

关于swift - 如何从系统函数的完成处理程序闭包中返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48523154/

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