简体   繁体   中英

Swift 2.0 migration error with alamofire

I am trying to use this piece of code I got raywenderlich.com in Xcode 7. But at the return line is giving me error saying

Cannot convert return expression of type (NilLiteralConvertible, NilLiteralConvertible) to return type Result<UIImage>

extension Alamofire.Request {
  public static func imageResponseSerializer() -> GenericResponseSerializer<UIImage> {
    return GenericResponseSerializer { request, response, data in
      if data == nil {
        return (nil, nil)
      }

      let image = UIImage(data: data!, scale: UIScreen.mainScreen().scale)

      return (image, nil)
    }
  }

  public func responseImage(completionHandler: (NSURLRequest, NSHTTPURLResponse?, UIImage?, NSError?) -> Void) -> Self {
    return response(responseSerializer: Request.imageResponseSerializer(), completionHandler: completionHandler)
  }
}

See original code at http://www.raywenderlich.com/85080/beginning-alamofire-tutorial

It looks like when you converted your project to Swift 2 you also upgraded to AlamoFire 2.x. The tutorial was written for Swift 1.2 where the closure's signature was:

(NSURLRequest?, NSHTTPURLResponse?, NSData?) -> (SerializedObject?, NSError?)

With AlamoFire 2 the signature is now:

(NSURLRequest?, NSHTTPURLResponse?, NSData?) -> Result<SerializedObject>

This means your method needs to return .Success(image!) in the passing condition and .Failure(data, myError) in the failing condition. It also means you can't just pass image without unwrapping since that initializer is nullable and the result's parameter is not.

Your serializer could look something like this:

return GenericResponseSerializer { request, response, data in
    guard let validData = data else {
        let error = ...
        return .Failure(data, error)
    }

    guard let image = UIImage(data: validData, scale: UIScreen.mainScreen().scale) else {
        let error = ...
        return .Failure(data, error)
    }

    return .Success(image)
}

For your error you could either define your own ErrorType enum that will be helpful to you or use AlamoFire.Error :

let error = Error.errorWithCode(.DataSerializationFailed, failureReason: "Image parsing failed.")

Your responseImage function will need a similar change:

public func responseImage(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<UIImage>) -> Void) -> Self {
    return response(responseSerializer: Request.imageResponseSerializer(), completionHandler: completionHandler)
}

This will in turn require you to update code that uses responseImage but those error messages should be helpful.

It work for me, remove old Alamofire from Ray's sample, and add last version form git https://github.com/Alamofire/Alamofire , and change sample XMLResponseSerializer , for UIImage it looks like :

extension Alamofire.Request {
public static func imageResponseSerializer() -> ResponseSerializer<UIImage, NSError> {
    return ResponseSerializer { request, response, data, error in
        guard error == nil else { return .Failure(error!) }
        guard let validData = data else {
            let failureReason = "Image parsing failed."
            let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }
        guard let image = UIImage(data: validData, scale: UIScreen.mainScreen().scale) else {
            let failureReason = "Image format failed."
            let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
            return .Failure( error)
        }
        return .Success(image)
    }
}
public func responseImage(completionHandler: Response<UIImage, NSError> -> Void) -> Self {
    return response(responseSerializer: Request.imageResponseSerializer(), completionHandler: completionHandler)
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM