开关必须在 Xcode 12 内详尽无遗

Switch must be exhaustive in Xcode 12

在新 Xcode 12 中,我在权限范围内遇到了一个特殊错误。 Switch 必须详尽添加 .limited case。 虽然我添加了 .limited case 但它仍然显示此错误。我在使用开关盒的地方得到这个。你能为我指出正确的方向吗。我最近将我的项目从 Xcode 11 升级到 Xcode 12,所以在这里有点困惑。

func openPhotoController(item: InfoItem) {
    
    
    let status = PHPhotoLibrary.authorizationStatus()
    
    switch status {
    case .authorized:
        
        DispatchQueue.main.async
        {
            let photoLib = photoLibViewController()
            photoLib.delegate = self
            photoLibCropImage = false
            photoLib.modalTransitionStyle = .crossDissolve
            photoLib.modalPresentationStyle = .fullScreen
            photoLib.allowMultipleSelection = false
            self.present(photoLib, animated: true, completion: nil)
        }
        print("authorized")
    //handle authorized status
    case .denied, .restricted :
        
        print("denied")
        
        AlertManager.showAlertView(title: "Alert?", subtitle: "Please allow access to your photo library to use this functionality", showCancelButton: true, okButtonTitle: "Go to Settings", cancelButtonTitle: "Cancel") {
            
            if let url = URL(string:UIApplication.openSettingsURLString)
            {
                if UIApplication.shared.canOpenURL(url)
                {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        }
    //handle denied status
    case .notDetermined:
        // ask for permissions
        PHPhotoLibrary.requestAuthorization { status in
            switch status {
            case .authorized:
                
                print("authorized")
                
                DispatchQueue.main.async
                {
                    let photoLib = photoLibViewController()
                    photoLib.delegate = self
                    photoLibCropImage = false
                    photoLib.modalTransitionStyle = .crossDissolve
                    photoLib.modalPresentationStyle = .fullScreen
                    photoLib.allowMultipleSelection = false
                    self.present(photoLib, animated: true, completion: nil)
                }
            // as above
            case .denied, .restricted:
                
                print("denied")
            // as above
            case .notDetermined:
                print("notDetermined")
            // won't happen but still
            case .limited:
                print("notDetermined")
            default:
                print("notDetermined")
            }  
        }
    }
}

为外部开关添加 .limited 外壳。

如果你不想处理它,你总是可以插入一个默认情况(你有你的内部开关,但没有外部开关):

...
default:
    // Otherwise, do something else

Swift 开关上的文档:https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html

WWDC 2020iOS 14 Apple 推出了一项新功能,该功能将提供对照片库的有限访问权限。您缺少外部主开关的 .limited 个外壳。您的更新代码:

switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
case .notDetermined:
    // ask for access
case .restricted, .denied:
    // sorry
case .authorized:
    // we have full access
 
// new option: 
case .limited:
    // we only got access to a part of the library
}

更多可以查看here