しおメモ

雑多な技術系ブログです。ニッチな内容が多いです。

Obj-Cライブラリ由来のNSExceptionのSwiftでのハンドリング

ずばりこれ。
NSSetUncaughtExceptionHandler(_:) - Foundation | Apple Developer Documentation

swiftのcatchで拾えないものもこちらでハンドルできる。
AppDelegate等に入れておく。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    NSSetUncaughtExceptionHandler { exception in
        print(exception)
        fatalError() // 適当な処理の後落とす
    }
    return true
}

ただ処理を継続しようとしても落ちるので、fatalError等できちんと落とす。