[SwiftUI 오류] Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value + 0 (:0) 오류 해결 하는 방법
내가 마든 첫 번째 ios 맥 어플에서 오류가 보고 되었다. 오류 내용은 다음과 같다.
Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value + 0 (:0)
총 5개의 오류보고가 있는데 모든 로그 파일을 열어보니 오류 내용이 모두 동일하다.
오류가 발생한 코드 위치는 다음과 같다.
....이상 생략
var someWindow: NSWindow!
....이하 생략    
someWindow = nil
someWindow = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 330, height: 230), styleMask: [.titled]  //, .closable .miniaturizable, .resizable, .fullSizeContentview
                    ,  backing: .buffered, defer: false)
...이하생략
func closeAlarmWindow() {
        SoundSetting.instance.onStopSound()
        someWindow.close()  //오류 발생 위치는 여기이다.
//        NSApplication.shared.keyWindow?.close()
    }
오류 해결
코드 실행전에 nil 체크 조건을 추가하였다. someWindow?.close() 코드로 적용해보았으나 동일한 오류가 발생되었다.
    func closeAlarmWindow() {
        SoundSetting.instance.onStopSound()
        if (someWindow != nil) {
            someWindow.close()
        }
    }
Optional의 값 추출 방식
- 강제 추출(Forced Unwrapping/ Unconditional Unwrapping)
 - 옵셔널 바인딩 (Optional Binding)
 - 암시적 추출 옵셔널 (Implicitly Unwrapped Optional)
 
강제 추출 (Forced Unwrapping/ Unconditional Unwrapping)
- Optional로 선언한 상수/변수에 값이 있음이 확실할 때 !를 통해 강제 추출 가능.
 - 강제 추출 시 반환 값이 nil이면 런타임 오류가 발생.
 
let a: Int? // 자동으로 nil 할당
print(a!) // Fatal error: Unexpectedly found nil while unwrapping an Optional value
[reference] 

