消除强制展开的最佳方法
Best way to remove force unwrapping
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
})
})
}
}
}
我们如何去除强制展开?
您可以使用 if let
(或者 guard let
)处理的第一个问题:
if let recorder = recorder {
// here it is unwrapped
} else {
// handle nil case
}
在第二个中我会使用默认值:
if (recorder?.isRunning() ?? false) {
// ...
}
但是,我看到您已经在第二种情况下使用了 if let
,因此您不需要它。所以我认为在这两种情况下最好使用 if let
,所以可能是这样的:
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
})
})
}
你可以让 recorder
变得懒惰。
var recorder: Recorder = {
return Recorder()
}()
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
})
})
}
}
}
我们如何去除强制展开?
您可以使用 if let
(或者 guard let
)处理的第一个问题:
if let recorder = recorder {
// here it is unwrapped
} else {
// handle nil case
}
在第二个中我会使用默认值:
if (recorder?.isRunning() ?? false) {
// ...
}
但是,我看到您已经在第二种情况下使用了 if let
,因此您不需要它。所以我认为在这两种情况下最好使用 if let
,所以可能是这样的:
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
})
})
}
你可以让 recorder
变得懒惰。
var recorder: Recorder = {
return Recorder()
}()