在 iOS 中等待异步函数的结果

Wait for result of async function in iOS

我有一个与我的代码相关的问题:

func isNotificationsEnabled()->Bool{
    var isNotificationEnabled = false
    center.getNotificationSettings() { (settings) in
        switch settings.soundSetting{
        case .enabled:
            isNotificationEnabled = true
           break
        case .disabled:
             isNotificationEnabled = false
             break

        case .notSupported:
            isNotificationEnabled = false
             break
        }
    }

     return isNotificationEnabled
}

此函数 return 结果先于 center.getNotificationSettings() returns 结果。有什么方法可以等待 center.getNotificationSettings() 的结果并同步此功能?

您要找的是 iOS 中的 Completion block, 试试这个,

func isNotificationsEnabled(completion:@escaping (Bool)->Swift.Void){
        var isNotificationEnabled = false
        center.getNotificationSettings() { (settings) in
            switch settings.soundSetting{
            case .enabled:
                isNotificationEnabled = true
                completion(isNotificationEnabled)
                break
            case .disabled:
                isNotificationEnabled = false
                completion(isNotificationEnabled)
                break

            case .notSupported:
                isNotificationEnabled = false
                completion(isNotificationEnabled)
                break
            }
        }
    }

用法,

isNotificationsEnabled { (isNotificationEnabled) in
    debugPrint(isNotificationEnabled)           
}

添加完成处理程序!

func isNotificationsEnabled(completion: (Bool) -> ())->Bool{
    var isNotificationEnabled = false
    center.getNotificationSettings() { (settings) in
        switch settings.soundSetting{
        case .enabled:
            isNotificationEnabled = true
           break
        case .disabled:
             isNotificationEnabled = false
             break

        case .notSupported:
            isNotificationEnabled = false
             break
        }
    }

     return isNotificationEnabled
     completion(isNotificationEnabled)
}

然后调用它

isNotificationEnabled() { isNotificationsEnabled in
   print(isNotificationsEnabled)
}

这是一个使用完成块的示例,虽然有所缩减,但与您的代码具有相同的功能:

func isNotificationsEnabled(completion:@escaping (Bool)->() ) {
    center.getNotificationSettings() { (settings) in
        switch settings.soundSetting {
        case .enabled:
            completion(true)

        default:
            completion(false)
        }
    }
}

这还可以简化为本质:

func isNotificationsEnabled(completion:@escaping (Bool)->() ) {
    center.getNotificationSettings() { (settings) in
        completion (settings.soundSetting == .enabled)
    }
}

因为只有 .enabled 情况 returns true 在所有其他情况下使用 default 到 return false。顺便说一下:在 Swift 中不需要 break 语句。

并称​​它为:

isNotificationsEnabled { success in
    if success {
        print("is enabled")
    } else {
        print("is disabled")
    }
}

仅供参考,您或许可以进一步简化:

func isNotificationsEnabled(completionHandler: @escaping (Bool) -> Void) {
    center.getNotificationSettings { settings in
        completionHandler(settings.soundSetting == .enabled)
    }
}

正如其他人指出的那样,它被调用为:

isNotificationsEnabled { enabled in
    if enabled {
        print("is enabled")
    } else {
        print("is not enabled")
    }
}