'sd_setImage(with:placeholderImage:completed:)' 与 Swift 的用法不明确 3
Ambiguous use of 'sd_setImage(with:placeholderImage:completed:)' with Swift 3
我在我的 imageView
上使用 SDWebImage
进行以下调用,它在 Swift 2
上运行良好,但在 XCode 8 beta 5
与 [=17= 编译时出错]:
imageView.sd_setImage(with:url, placeholderImage:placeholder, completed: {
(image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
...
});
错误是:
Ambiguous use of 'sd_setImage(with:placeholderImage:completed:)'
我怀疑完成的处理程序的签名有问题,但我无法弄清楚语法应该是什么。我错过了什么?
Swift 编译器将 ObjC headers 翻译成 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.
将 SDWebImageOptions
添加到方法调用修复了问题:
imageView.sd_setImage(with: someUrl,
placeholderImage: someImage,
options: [],
completed: someCompletitionBlock)
我在我的 imageView
上使用 SDWebImage
进行以下调用,它在 Swift 2
上运行良好,但在 XCode 8 beta 5
与 [=17= 编译时出错]:
imageView.sd_setImage(with:url, placeholderImage:placeholder, completed: {
(image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
...
});
错误是:
Ambiguous use of 'sd_setImage(with:placeholderImage:completed:)'
我怀疑完成的处理程序的签名有问题,但我无法弄清楚语法应该是什么。我错过了什么?
Swift 编译器将 ObjC headers 翻译成 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.
将 SDWebImageOptions
添加到方法调用修复了问题:
imageView.sd_setImage(with: someUrl,
placeholderImage: someImage,
options: [],
completed: someCompletitionBlock)