在if语句中访问变量的值

时间:2017-01-26 23:52:41

标签: swift if-statement

我需要在if语句中访问变量的值,这是我的代码:

var codeError : Int?

@IBAction func mySwitch(_ sender: UISwitch) {
     if sender.isOn {
        codeError = 1
     } else {
        codeError = 2
     }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if codeError == 1 {
       front()
    } else if codeError == 2 {
       back()
    }

}

我的两个函数,这是我的front()函数:

    func front() {

    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified)

    for device in (deviceDiscoverySession?.devices)! {


        if device.position == AVCaptureDevicePosition.front {

            do {

                let input = try AVCaptureDeviceInput(device: device)
                if captureSession.canAddInput(input) {

                    captureSession.addInput(input)

                    if captureSession.canAddOutput(photoOutput) {

                        captureSession.addOutput(photoOutput)
                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer!)


                    }

                }

            } catch {

                print("Error")

            }

        }

    }

    captureSession.startRunning()
    print("")

}

和back()函数:

    func back() {

    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified)

    for device in (deviceDiscoverySession?.devices)! {


        if device.position == AVCaptureDevicePosition.back {

            do {

                let input = try AVCaptureDeviceInput(device: device)
                if cS.canAddInput(input) {

                    cS.addInput(input)

                    if cS.canAddOutput(photoOutput) {

                        cS.addOutput(photoOutput)
                        previewLayer = AVCaptureVideoPreviewLayer(session: cS)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                        cameraView.layer.addSublayer(previewLayer!)


                    }

                }

            } catch {

                print("Error")

            }

        }

    }

    cS.startRunning()
    print("")

}

问题出在我运行应用程序时。当我得到1 o 2时,我得到codeError的nil值。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

它的工作方式与原始代码类似。但是你在viewWillAppear内打印。但此时它并没有从你的uiswitch初始化。

一个解决方案:给它默认值:

var codeError: Int = 1

或更短

var codeError = 1

注意:

  

viewWillAppear中(_:)

     

通知视图控制器其视图即将添加到视图层次结构中。

请参阅https://developer.apple.com/reference/uikit/uiviewcontroller/1621510-viewwillappear

但是在您的UISwitch所在的视图上调用它。对于你的主要目标(隐藏在对另一个答案的评论中),打印变量

是错误的

答案 1 :(得分:0)

我不确定您在与viewWillAppear上的视图互动时访问用户设置的变量的原因。我建议花一些时间研究UIViewController的生命周期。

但是,如果您只想在开关更改值上打印错误代码,则应该这样做。

var codeError : Int?

@IBAction func mySwitch(_ sender: UISwitch) {
     if sender.isOn {
        codeError = 1
     } else {
        codeError = 2
     }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // will print nothing since errorCode is not set
    printCodeError()
}

func printCodeError() {
  if let codeError = self.codeError {
    print("\(codeError)")
  }
}