Alamofire: errorWithCode(_:failureReason:) is deprecated

Created on 11 May 2016  路  5Comments  路  Source: Alamofire/Alamofire

I just started getting this XCode warning tonight. Any way to resolve it?

question

Most helpful comment

You can use following code:

let userInfo: Dictionary<NSObject, AnyObject> = [NSLocalizedFailureReasonErrorKey: failureReason, Error.UserInfoKeys.StatusCode: response!.statusCode]
let error = NSError(domain: Error.Domain, code: Error.Code.StatusCodeValidationFailed.rawValue, userInfo: userInfo)

BTW, I guess that functions:

static func error(domain domain: String = Error.Domain, code: Code, failureReason: String) -> NSError

and

static func error(domain domain: String = Error.Domain, code: Int, failureReason: String) -> NSError

would be public in file Source/Error.swift.

All 5 comments

You can use following code:

let userInfo: Dictionary<NSObject, AnyObject> = [NSLocalizedFailureReasonErrorKey: failureReason, Error.UserInfoKeys.StatusCode: response!.statusCode]
let error = NSError(domain: Error.Domain, code: Error.Code.StatusCodeValidationFailed.rawValue, userInfo: userInfo)

BTW, I guess that functions:

static func error(domain domain: String = Error.Domain, code: Code, failureReason: String) -> NSError

and

static func error(domain domain: String = Error.Domain, code: Int, failureReason: String) -> NSError

would be public in file Source/Error.swift.

+1 that the the two functions @rafalkitta mentioned should be public..?

They were deprecated in #1166 to stop encouraging users to create their own errors with the Alamofire domain. The code the @rafalkitta posted will work, but we'd encourage you to use your own custom domain to have a clear separation between the custom errors you're creating and the ones that Alamofire creates.

The solution to fix the warnings is to stop using the errorWithCode(_:failureReason:) API and instead create your own NSError objects directly.

Cheers. 馃嵒

@DavidGagne I'd actually recommend that developers create their own ErrorType not based on NSError, since NSError's domain and code and rather useless. Instead, an enum, as described in that article, would work well, with each case capturing whatever state it needs to to be able to present errors to the user. But the article does outline an approach that makes this change less painful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhouhao27 picture zhouhao27  路  3Comments

Tulleb picture Tulleb  路  3Comments

sarbogast picture sarbogast  路  3Comments

Footjy picture Footjy  路  3Comments

lvandal picture lvandal  路  3Comments