53

I am trying to create an error object to display to the user.

let userInfo: [NSObject : AnyObject] = 
    [
    "NSLocalizedDescriptionKey" :  NSLocalizedString("Unauthorized", comment: ""),
    "NSLocalizedFailureReasonErrorKey" : NSLocalizedString("Unauthorized", comment: "")
    ]
let err = NSError(domain: "ShiploopHttpResponseErrorDomain", code: httpResponse.statusCode, userInfo: userInfo)
print("Error in Post: \(err.localizedDescription)")

Unfortunately the output is:

Error in Post: The operation couldn’t be completed.(ShiploopHttpResponseErrorDomain error 401.) 

I want to be able to show to the user that he should activate his account. Any ideas??

Ehab Saifan
  • 569
  • 1
  • 4
  • 8

3 Answers3

43

Looks like you want (see dictionary keys):

Swift 2

let userInfo: [NSObject : AnyObject] =
[
    NSLocalizedDescriptionKey :  NSLocalizedString("Unauthorized", value: "Please activate your account", comment: ""),
    NSLocalizedFailureReasonErrorKey : NSLocalizedString("Unauthorized", value: "Account not activated", comment: "")
]

Swift 3

let userInfo: [AnyHashable : Any] =
            [
                NSLocalizedDescriptionKey :  NSLocalizedString("Unauthorized", value: "Please activate your account", comment: "") ,
                NSLocalizedFailureReasonErrorKey : NSLocalizedString("Unauthorized", value: "Account not activated", comment: "")
        ]

Then create the error object in both swift 2 or 3 like this:

let err = NSError(domain: "ShiploopHttpResponseErrorDomain", code: 401, userInfo: userInfo)
println("Error in Post: \(err.localizedDescription)")

NSLocalizedDescriptionKey and NSLocalizedFailureReasonErrorKey are global String variables, and the keys inside of the userInfo dictionary. The values are slightly different from what you specified:

println(NSLocalizedDescriptionKey) //prints "NSLocalizedDescription"
println(NSLocalizedFailureReasonErrorKey) //prints "NSLocalizedFailureReason"

I find it good practice to look at the documentation by right-clicking the class (NSError in this case) and selecting "Jump To Definition" within xcode. All kinds of questions can be answered this way. :)

Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45
ProgrammierTier
  • 1,400
  • 10
  • 15
  • It might also make sense to create your own error class by inheriting from NSError and then overriding the getter for localizedDescription. – ProgrammierTier Oct 15 '15 at 21:29
  • The thing is I should subclass NSError to be able to custom "NSLocalizedDescriptionKey" to show my custom message. I am new to Swift and programming. So I will have to dig to know how to Implement this :) thx for your response – Ehab Saifan Oct 16 '15 at 16:31
40

Creating a very simple error in Swift 3:

let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Object does not exist"])
Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
21

Creating an error is as simple as following line:

let error = NSError(domain: "com.example.error", code: 0, userInfo: [NSLocalizedDescriptionKey: "message"])

If you need additional stack trace information, use following method:

func error(_ message: String, code: Int = 0, domain: String = "com.example.error", function: String = #function, file: String = #file, line: Int = #line) -> NSError {

    let functionKey = "\(domain).function"
    let fileKey = "\(domain).file"
    let lineKey = "\(domain).line"

    let error = NSError(domain: domain, code: code, userInfo: [
        NSLocalizedDescriptionKey: message,
        functionKey: function,
        fileKey: file,
        lineKey: line
    ])

    // Crashlytics.sharedInstance().recordError(error)

    return error
}

Usage:

let localizedErrorMessage: String = NSLocalizedString("Unauthorized", comment: "Account not activated")
let error = error(localizedErrorMessage)
AamirR
  • 11,672
  • 4
  • 59
  • 73