Swift将NSError转换为String

时间:2016-05-17 19:03:37

标签: swift

有没有办法将swift中NSError类型的错误转换为字符串?

例如对于这样的事情:

do {
    try response.status(.OK).send(fileName: "html/index.html")
} catch {
    response.status(.FAIL).send(error.CONVERT_TO_STRING)
}

.send()默认需要一个字符串。

2 个答案:

答案 0 :(得分:20)

如果您的错误类型为NSError,则可以使用error.localizedDescription来获取字符串。

答案 1 :(得分:1)

error.localizedDescription并不总是包含您需要的所有信息。在那种情况下,您可以选择将整个对象转换为字符串...

let description = "\(error)"

相关问题