Swift 3对'sd_setImage(with:placeholderImage:completed :)'的模糊使用

我在我的SDWebImage上使用SDWebImage进行了以下调用,这对于Swift 2工作正常,但是在使用Swift 3进行XCode 8 beta 5编译时出现错误:

  imageView.sd_setImage(with:url, placeholderImage:placeholder, completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in ... }); 

错误是:

模糊使用'sd_setImage(with:placeholderImage:completed :)'

我怀疑我在完成的处理程序的签名中出现了错误,但我无法弄清楚语法应该是什么。 我错过了什么?

Swift编译器将ObjC头文件翻译成Swift,从而导致命名冲突:

的UIImageView + WebCache.h:

 o1) - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock; o2) - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock; 

他们唯一的区别是o2中的附加options参数。

生成的Swift声明:

 s1) open func sd_setImage(with url: URL!, placeholderImage placeholder: UIImage!, completed completedBlock: SDWebImage.SDWebImageCompletionBlock!) s2) open func sd_setImage(with url: URL!, placeholderImage placeholder: UIImage!, options: SDWebImageOptions = [], completed completedBlock: SDWebImage.SDWebImageCompletionBlock!) 

因为options被翻译成可选参数(默认分配一个空数组),所以在Swift中调用s1会导致模糊的使用。 调用s2可以简单地具有相同的实现。 当在Swift代码中提供这样的方法时,可以在单个函数实现中将options参数添加为可options

解决方法

作为解决方法,可以设置options参数,或者可以临时将o1o2重命名,直到SDWebImage将被转换为Swift。

SDWebImageOptions添加到方法调用可以解决问题:

 imageView.sd_setImage(with: someUrl, placeholderImage: someImage, options: [], completed: someCompletitionBlock)