【问题标题】:Refactor code for two switch case statement重构两个 switch case 语句的代码
【发布时间】:2021-10-13 16:44:52
【问题描述】:

我正在处理一个 swift 项目,并且有一个问题要重构我的代码。

我正在编写代码来检查用户是否在应用程序中同时授权了摄像头和麦克风授权。我编写了以下代码,但我想(并且我希望)我可以重构代码,因为我认为下面的代码不清楚。我了解了基本的 Swift 语法,并逐渐了解了编译语言,但请告诉我是否有办法使其更具可读性或更易于编写。

我想在这里做的是......

  • 检查摄像头和麦克风是否都被授权。

  • 如果两者都被授权,则使用 showNextVC() 显示视图控制器。

  • 如果其中一项或两项均未获得授权,则使用 showConfigurationAlert 显示警报

    func checkAuthStatus(){
        checkCameraStatus()
    }
    
    func checkCameraStatus() {
        switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .notDetermined:
            print("not Determined")
            AVCaptureDevice.requestAccess(for: .video) { granted in
                if granted {
                    print("Now it's granted")
                }
            }
        case .restricted:
            print("restricted")
            showConfigurationAlert(for: "camera")
        case .denied:
            print("denied")
            showConfigurationAlert(for: "camera")
        case .authorized:
            checkMicrophoneStatus()
        @unknown default:
            print("unknown")
        }
    
    }
    
    func checkMicrophoneStatus() {
        switch AVCaptureDevice.authorizationStatus(for: .audio){
        case .notDetermined:
            AVCaptureDevice.requestAccess(for: .audio) { granted in
                if granted {
                    print("Now it's granted")
                }
            }
        case .restricted:
            print("restricted")
            showConfigurationAlert(for: "microphone")
        case .denied:
            print("denied")
            showConfigurationAlert(for: "microphone")
        case .authorized:
            print(("authorized"))
            showNextVC()
        @unknown default:
            print("unknown")
        }
    }
    

我所做的是,首先在 checkAuthStatus 中检查相机授权,然后,如果相机被授权,则触发 checkMicrophoneStatus() 以检查麦克风授权。 我认为这段代码不清楚的原因是,我只在 checkAuthStatus() 函数中编写了一个检查相机授权的函数。我想如果我能写出类似的东西很清楚

func checkAuthStatus(){
    // check both cameara and microphone is authorized.
    // if both of them are authorized, show next VC with showNextVC() function.
}

【问题讨论】:

  • 如果摄像头授权状态未确定,则请求许可,然后checkCameraStatus会立即返回,对麦克风不做任何操作,无论用户选择什么选项。这是故意的吗?

标签: ios swift switch-statement refactoring


【解决方案1】:

一种方法是为这两个函数添加一个onAuthorised 闭包参数。这两个checkXXXStatus 函数也有很多共同点。我们不需要重复 switch 语句。

func checkMediaStatus(type: AVMediaType, deviceName: String, onAuthorised: (() -> Void)?) {
    switch AVCaptureDevice.authorizationStatus(for: type){
    case .notDetermined:
        AVCaptureDevice.requestAccess(for: type) { granted in
            if granted {
                print("Now it's granted")
            }
        }
    case .restricted:
        print("restricted")
        showConfigurationAlert(for: deviceName)
    case .denied:
        print("denied")
        showConfigurationAlert(for: deviceName)
    case .authorized:
        print(("authorized"))
        onAuthorised?()
    @unknown default:
        print("unknown")
    }
}

func checkMicrophoneStatus(onAuthorised: (() -> Void)?) {
    checkMediaStatus(type: .audio, deviceName: "microphone", onAuthorised: onAuthorised)
}

func checkCameraStatus(onAuthorised: (() -> Void)?) {
    checkMediaStatus(type: .video, deviceName: "camera", onAuthorised: onAuthorised)
}

那么checkAuthStatus可以写成:

func checkAuthStatus(){
    checkCameraStatus {
        self.checkMicrophoneStatus {
            self.showNextVC()
        }
    }
}

还请注意,您可能还想在 requestAccess 完成处理程序中调用 onAuthorisedshowConfigurationAlert。我认为这是一个更好的设计。

if granted {
    print("Now it's granted")
    onAuthorised?()
} else {
    showConfigurationAlert(for: deviceName)
}

【讨论】:

  • 非常感谢!我使用了您的示例,并且只能编写一个 switch 语句。还。这是我第一次使用一个对其参数进行闭包的函数。学习很棒!
【解决方案2】:

我认为您正在寻找这样的东西:

let videoStatus = AVCaptureDevice.authorizationStatus(for: .video)
let audioStatus = AVCaptureDevice.authorizationStatus(for: .audio)

switch (videoStatus, audioStatus) {
case (.authorized, .authorized): showNextVC()
case (.authorized, _): showConfigurationAlert(for: "microphone")
case (_, .authorized): showConfigurationAlert(for: "camera")
default: print(videoStatus, audioStatus)
}

您可以同时检查两种情况并处理您喜欢的每种情况

【讨论】:

  • 非常感谢您的回复!我刚刚了解到我可以为 switch 语句使用两个参数!而这个例子正是我想做的!
【解决方案3】:

您可以更改 checkCameraStatus() 和 checkMicrophoneStatus() 以返回 bool 并在 checkAuthStatus() 内部更改为:

func checkAuthStatus(){
    let cameraAuthorised = checkCameraStatus()
    let micAuthorised = checkMicrophoneStatus()

    if (cameraAuthorised && micAuthorised) {
        showNextVC()
    }
 }

【讨论】:

  • 非常感谢您的快速回复!我只是用了一个不同的例子,但我知道这也有效!谢谢你告诉我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多