代码之家  ›  专栏  ›  技术社区  ›  Sanoj Kashyap

消除强制展开的最佳方法

  •  -1
  • Sanoj Kashyap  · 技术社区  · 6 年前
    var recorder : Recorder? = nil
    
    func startAudioRecording() {
        if recorder == nil {
            recorder = Recorder()
        }
        if !recorder!.isRunning() {
            recorder?.startRecording({ [weak self] audioData in
                self?.remoteInterface?.sendVoice(audioData.0)
            }, withCompletionBlock: { (_) in })
        }
    }
    
    func stopAudioRecording(_ keyCommand: String!){
        if let _ = recorder {
            if(recorder?.isRunning())! {
                recorder?.stopRecording(completionBlock: { (isFinished: Bool) in
                    DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * NSEC_PER_MSEC)) / Double(NSEC_PER_SEC), execute: { [unowned self] in
                        self.remoteInterface?.sendTouchUp(keyCommand)
                        self.audioRecorder = nil
                    })
                })
            }
        }
    }
    

    如何消除力展开?

    2 回复  |  直到 6 年前
        1
  •  5
  •   Milan Nosáľ    6 年前

    您可以使用的第一个 if let (或者 guard let ):

    if let recorder = recorder {
        // here it is unwrapped
    } else {
        // handle nil case
    }
    

    在第二个示例中,我将使用默认值:

    if (recorder?.isRunning() ?? false) {
        // ...
    }
    

    但是,我看到您已经在使用 如果让 在第二种情况下,你不需要这个。所以我认为最好是 如果让 在这两种情况下,可能是这样的:

    var recorder : Recorder? = nil
    
    func startAudioRecording(){
        if recorder == nil {
            recorder = Recorder()
        }
        if let recorder = recorder,
            !recorder.isRunning() {
    
            recorder.startRecording({ [weak self] audioData in
                self?.remoteInterface?.sendVoice(audioData.0)
            }, withCompletionBlock: { (_) in })
        }
    }
    
    func stopAudioRecording(_ keyCommand: String!){
        if let recorder = recorder,
            recorder.isRunning() {
    
            recorder.stopRecording(completionBlock: { (isFinished: Bool) in
                DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * NSEC_PER_MSEC)) / Double(NSEC_PER_SEC), execute: { [unowned self] in
                    self.remoteInterface?.sendTouchUp(keyCommand)
                    self.audioRecorder = nil
                })
            })
        }
    }
    

    或者,如@Fogmaster所述,使用 guard 在第二种方法中,可能看起来更好一些:

    func stopAudioRecording(_ keyCommand: String!){
        guard let recorder = recorder,
            recorder.isRunning() else {
            return
        }
    
        recorder.stopRecording(completionBlock: { (isFinished: Bool) in
            DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double(Int64(1 * NSEC_PER_MSEC)) / Double(NSEC_PER_SEC), execute: { [unowned self] in
                self.remoteInterface?.sendTouchUp(keyCommand)
                self.audioRecorder = nil
            })
        })
    }
    
        2
  •  1
  •   Samah    6 年前

    你可以 recorder 而是懒惰。

    var recorder: Recorder = {
        return Recorder()
    }()