Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift creating NSError Object

Tags:

ios

swift

nserror

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??

like image 435
Ehab Saifan Avatar asked Oct 15 '15 21:10

Ehab Saifan


People also ask

Which is used when creating a NSError object?

Creating Error Objectsinit(domain: String, code: Int, userInfo: [String : Any]?) Returns an NSError object initialized for a given domain and code with a given userInfo dictionary.

Does NSError conform to error?

It consists of a predefined error domain, a domain-specific error code, and a user info dictionary containing application-specific information. Error is a Swift protocol which classes, structs and enums can and NSError does conform to. A type representing an error value that can be thrown.

How do I create a custom error in Swift?

To add a description to a new error type, extend the custom error to conform to CustomStringConvertible and add a property description : // For each error type return the appropriate description extension CustomError: CustomStringConvertible { public var description: String { switch self { case .

How do you convert error to NSError?

It should be possible to turn an arbitrary Swift enum that conforms to Error into an NSError by using the qualified type name as the domain key, the enumerator as the error code, and turning the payload into user data.


1 Answers

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. :)

like image 127
ProgrammierTier Avatar answered Sep 22 '22 15:09

ProgrammierTier