alex
alex

Reputation: 4914

Alamofire, error message Use of undeclared type 'GenericResponseSerializer'

I follow an alamofire/swift turorial by ray wanderlich and i use swift 2.0 and the alamofire 3.0 beta3. Now i have handled some minor issues but this one i am stuck with

  message: Use of undeclared type 'GenericResponseSerializer'
 Generic type 'Result' specialized with too few type parameters (got 1, but expected 2)

and this is part of my code

extension Alamofire.Request {

    /** Response serializer for images from: http://www.raywenderlich.com/85080/beginning-alamofire-tutorial */
    class func imageResponseSerializer() -> GenericResponseSerializer<UIImage> {
        return GenericResponseSerializer { request, response, data in

            guard let validData = data else {
                let failureReason = "Data could not be serialized. Input data was nil."
                let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
                return .Failure(data, error)
            }

            if let image = UIImage(data: validData, scale: UIScreen.mainScreen().scale) {
                return Result<UIImage>.Success(image)
            }
            else {
                return .Failure(data, Error.errorWithCode(.DataSerializationFailed, failureReason: "Unable to create image."))
            }

        }
    }

    /** Convenience method for returning images from: http://www.raywenderlich.com/85080/beginning-alamofire-tutorial */
    func responseImage(completionHandler: (NSURLRequest?, NSHTTPURLResponse?, Result<UIImage>) -> Void) -> Self {
        return response(responseSerializer: Request.imageResponseSerializer(), completionHandler: completionHandler)
    }
}

ps: where/how can i download Alamofire 2.0 ??

Upvotes: 2

Views: 2566

Answers (3)

Jack Berstrem
Jack Berstrem

Reputation: 535

I for one don't fully understand the logic of the GenericResponseSerializer. So what I did for my app (because I do need this functionality) is just completely comment out the code that you just wrote in your post (I followed the same Ray Wenderlich tutorial as you) and then in my PhotoBrowserCollectionViewController, I changed things around a bit in my cellForItemAtIndexPath function. It looks like this now:

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as! PhotoBrowserCollectionViewCell

    let imageURL = (photos.objectAtIndex(indexPath.row) as! PictureElement).imageURL

    cell.request?.cancel()

    // Using image cache system to make sure the table view works even when rapidly scrolling down the screen.
    if let image = self.imageCache.objectForKey(imageURL) as? UIImage {
        cell.imageView.image = image

    } else {

        cell.imageView.image = nil


        cell.request = Alamofire.request(.GET, imageURL).responseImage { response in
            debugPrint(response)

            print(response.request)
            print(response.response)
            debugPrint(response.result)

            if let image = response.result.value {

                self.imageCache.setObject(response.result.value!, forKey: response.request!.URLString)
                cell.imageView.image = image
            } else {

            }


        }
}
return cell

Still haven't gotten it to fully work (because my API part doesn't work at all), so maybe this is completely wrong, but at least it builds. I used AlamofireImage for this, just using the basic explanations they have on their GitHub page, linked by the first guy who responded to this post. At least use that and see where it gets you.

Upvotes: 0

cnoon
cnoon

Reputation: 16643

The example does not work with Alamofire 3.0. The GenericResponseSerializer logic has been heavily refactored. Please check out our latest README and 3.0 Migration Guide.


EDIT

Additionally, you should check out AlamofireImage which contains all the logic for response image serialization as well as many other cool features. It is also built by the AlamofireSF and will be well supported moving forwards.

Upvotes: 1

Bassebus
Bassebus

Reputation: 732

Have you tried to use AlamofireImage? Seems like a nice extension. Alamofire 2.0.2, but you should test to install via Carthage, really simple and clean tool.

Upvotes: 1

Related Questions