【问题标题】:Switch must be exhaustive in Xcode 12Xcode 12 中的 Switch 必须详尽无遗
【发布时间】:2020-09-17 04:56:27
【问题描述】:

在新的 Xcode 12 中,我在权限范围内遇到了一个特殊错误。 开关必须详尽,添加 .limited 大小写。 虽然我添加了 .limited 大小写,但它仍然显示此错误。我在使用开关盒的地方得到这个。你能为我指出正确的方向吗。我最近将我的项目从 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")
            }  
        }
    }
}

【问题讨论】:

  • 有2个开关。外部开关(父开关)没有'case .limited'。
  • thanks 有点惊慌,忘记了基础

标签: swift xcode12


【解决方案1】:

WWDC 2020 中,iOS 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

【讨论】:

    【解决方案2】:

    为外部开关添加.limited case。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      相关资源
      最近更新 更多