首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift 5中的并发竞争条件函数

Swift 5中的并发竞争条件函数
EN

Stack Overflow用户
提问于 2020-03-12 21:03:30
回答 1查看 145关注 0票数 0

我有下面的代码,它应该返回NotificationCenter的设置,但是当我运行这段代码时,变量notificationSetting什么也不返回。

如何解决这个问题,以便应用程序等待结果?

代码语言:javascript
复制
func getNotificationSetting() -> String{
    var notificationSetting = ""
    
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        switch settings.authorizationStatus {
        case .authorized, .provisional:
            notificationSetting = "Authorized"
        case .denied:
            notificationSetting = "Denied"
        case .notDetermined:
            notificationSetting = "NotDetermined"
        @unknown default:
            notificationSetting = "NotDetermined"
        }
    }
    
    return notificationSetting
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 21:25:51

getNotificationSettings异步执行。

代码语言:javascript
复制
func getNotificationSetting(completionHandler: @escaping (String) -> Void) {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        switch settings.authorizationStatus {
        case .authorized, .provisional:
            completionHandler("Authorized")
        case .denied:
            completionHandler("Denied")
        case .notDetermined:
            completionHandler("NotDetermined")
        @unknown default:
            completionHandler("NotDetermined")
        }
    }
}

func getSettings() {
    self.getNotificationSetting(completionHandler: { (notificationSetting) in
        // do what you want
        print(notificationSetting)
    })
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60661840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档