我正在使用SDWebImage
上的imageView
进行以下调用,该调用可与Swift 2
一起正常工作,但使用XCode 8 beta 5
编译Swift 3
时出现错误:
imageView.sd_setImage(with:url, placeholderImage:placeholder, completed: {
(image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
...
});
错误是:
我怀疑完成的处理程序的签名中有问题,但是我无法弄清楚语法应该是什么。我想念什么?
最佳答案
Swift编译器将ObjC header 转换为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
参数,或者可以临时重命名o1或o2,直到将SDWebImage转换为Swift为止。