如何在swift中删除重复的代码?

时间:2017-08-19 18:14:15

标签: swift

我有2个部分和6行代码,它们为我为项目制作的应用做了相反的事情。代码是按照它教给我们的方式输入的,但在项目审查中,我被告知要删除重复/类似的代码。任何人都可以帮我指出正确的方向吗?感谢

 @IBAction func stopRecording(_ sender: AnyObject) {
        recordButton.isEnabled = true
        stopRecordingButton.isEnabled = false
        recordingLabel.text = "Tap to Record"
   }


 @IBAction func recordAudio(_ sender: AnyObject)
        {
            recordingLabel.text = "Recording in Progress"
            stopRecordingButton.isEnabled = true
            recordButton.isEnabled = false
      }

1 个答案:

答案 0 :(得分:2)

您也可以didSet观察员。

执行此操作
var isRecording: Bool = false {
    didSet {
        recordButton.isEnabled = !isRecording
        stopRecordingButton.isEnabled = isRecording
        recordingLabel.text = isRecording ? "Recording in progress" : "Tap to Record"
    }
}

这样的动作变得非常简单。

@IBAction func stopRecording(_ sender: AnyObject) {
    isRecording = false
}


@IBAction func recordAudio(_ sender: AnyObject) {
    isRecording = true
}