【发布时间】:2016-11-12 19:17:15
【问题描述】:
我想请求一些权限并且想要返回 false,除非所有权限都已被授予。 我收到一个错误: "void 函数中出现意外的非 void 返回值"
但我在所有情况下都返回真/假,有什么问题?
func requestPermissions () -> Bool {
let types: UIRemoteNotificationType = [.alert, .badge, .sound]
UIApplication.shared.registerForRemoteNotifications(matching: types)
let center = UNUserNotificationCenter.current()
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
if (videoGranted) {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
if (audioGranted) {
center.requestAuthorization(options: [.alert, .badge, .sound]) { (notificationGranted:Bool, error) -> Void in
if (notificationGranted) {
DispatchQueue.main.async {
return true
print("Video, audio & notifications granted")
}
} else {
return false
print("Rejected notifications")
}
}
}else {
return false
print("Rejected audio")
}
})
} else {
return false
print("Rejected video")
}
})
}
任何帮助将不胜感激!谢谢。
另一种答案:
func requestPermissionss (completion: ((Bool, Bool, Bool)->Void)?) {
let types: UIRemoteNotificationType = [.alert, .badge, .sound]
UIApplication.shared.registerForRemoteNotifications(matching: types)
let center = UNUserNotificationCenter.current()
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
center.requestAuthorization(options: [.alert, .badge, .sound]) { (notificationGranted:Bool, error) -> Void in
completion?(videoGranted, audioGranted, notificationGranted)
}
})
})
}
【问题讨论】:
-
else { print("Rejected notifications") has no status=true
-
@alldani-com 嗨,我复制了错误的功能,对不起。更新的问题。
-
在 return 语句之后 swift 是否仍然运行?我知道python没有。
-
查找“如何从异步函数返回值”
-
尝试将打印语句放在返回语句之前。