ぜのぜ

しりとりしようぜのぜのぜのぜ

localizedDescriptionに30分消えた

こう書くとerrorDescriptionの型はStringになる.

import Foundation

struct MyError: LocalizedError {
    var errorDescription = "myError description"
}

print(MyError().localizedDescription) // The operation couldn’t be completed. (__lldb_expr_183.MyError error 1.)

一方LocalizedErrorerrorDescriptionの型はString?なので,localizedDescription内ではデフォルト実装の方のerrorDescription: Stirng?が使われる.そのためThe operation couldn’t be completed. (__lldb_expr_183.MyError error 1.)が表示された.

一応書いておくが,こう書くと期待通りに動く.

import Foundation

struct MyError: LocalizedError {
    var errorDescription: String? = "myError description"
}

print(MyError().localizedDescription) // myError description