How to integrate latest SDWebImage API in my Swift based project?

Developer picture Developer · Nov 10, 2014 · Viewed 23k times · Source

I have used SDWebImage with Objective C and it worked great for me but now I am learning Swift and trying to integrate the latest version of the API but I am stucking at every step as API is in Objective C and there are no steps mentioned to use the API with Swift. I read the documents and created the bridge header file and included the required file like below:

#ifndef MyProject_Bridging_Header_h
#define MyProject_Bridging_Header_h

#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"

#endif

I have added the frameworks as well and dragged the SDWebImage project within my app as explained here

I am really struggling in this. Please help! For reference I have added an image showing the error!enter image description here

Answer

Ghislaindj picture Ghislaindj · Dec 23, 2014

Here is a code example that should work :

let block: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
    println(self)
}

let url = NSURL(string: "http://placehold.it/350x150")

self.imageView.sd_setImageWithURL(url, completed: block)

and in your bridging header file :

#import "UIImageView+WebCache.h"

So your bridging header file should work, but sometimes I had trouble with the bridging header and in theses cases I just delete it, and add it again and everything works fine after.