Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift convert NSError to String

Tags:

swift

Is there a way to convert an error of type NSError in swift to a string?

For example for something like this:

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

.send() expects a string by default.

like image 510
eclipse Avatar asked May 17 '16 19:05

eclipse


2 Answers

If your error is of type NSError, you can use error.localizedDescription to get the string.

like image 125
sakurasunris3 Avatar answered Sep 18 '22 19:09

sakurasunris3


error.localizedDescription doesn't always contain all the information you need. In that instance you do have the option of converting the whole object to a string...

let description = "\(error)"

like image 35
Oliver Pearmain Avatar answered Sep 20 '22 19:09

Oliver Pearmain