Search code examples
swifttypedefsdwebimage

SDWebImage and Swift


I've succeeded in incorporating SDWebImage (written in Objective-C) with my Swift project - but its still acting a bit funny. Specifically, its giving me an error in the if statement inside the following Closure:

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

        if (image && cacheType == SDImageCacheType.SDImageCacheTypeNone) {
            cell.productImageView.alpha = 0.0
            UIView.animateWithDuration(1.5, animations: {
                cell.productImageView.alpha = 1.0
            })
        }
    }

    cell.productImageView.sd_setImageWithURL(imageURL!, placeholderImage:UIImage(named:"Icon120pix.png"), completed: completionBlock)

The error I'm getting on that if statement is: Use of unresolved identifier 'SDImageCacheTypeNone'

This makes no sense because SDImageCacheTypeNone is one of the values defined in the SDImageCacheType typedef.

By the way, if I take that if statement out and leave just the statements inside it, everything works just fine.

So any ideas what I might be doing wrong here?


Solution

  • Have a look at this document in the Enumerations section for a more in depth explanation as to why Swift displays Objective-C enums differently.

    Apple Swift Documents

    In your code example the way to solve the problem is to use:

    SDImageCacheType.None
    

    Instead of

    SDImageCacheType.SDImageCacheTypeNone