ios - 'sd_setImage(with:placeholderImage:completed:)' 与 Swift 3 的不明确使用

标签 ios sdwebimage swift3 xcode8

我在我的 imageView 上使用 SDWebImage 进行以下调用,它在 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
            ...
    });

错误是:

Ambiguous use of 'sd_setImage(with:placeholderImage:completed:)'

我怀疑已完成的处理程序的签名有问题,但我无法弄清楚语法应该是什么。我错过了什么?

最佳答案

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 参数,或者可以临时重命名 o1o2,直到 SDWebImage 被翻译成 Swift。

关于ios - 'sd_setImage(with:placeholderImage:completed:)' 与 Swift 3 的不明确使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38949214/

相关文章:

uitableview - 使用 Storyboard中的 SDWebImage 在自动布局中查看图像

ios - 使用适用于 iOS Swift 3 的 REST API 从 AZURE 下载文件

ios - 我如何调配 corefoundation 框架方法?

jquery - 链接不会在带有 PhoneGap 的 JQuery Mobile 的外部浏览器中打开

ios - Google map 地址自动完成和地理编码 API 返回差异地址

ios - 限制对 Firebase 存储的访问,以便只有我的应用可以访问它

swift - 为什么我不能让 SDWebImage 与 swift 一起工作?

ios - 避免我的 iOS 应用程序中的文件易于替换

iOS 11 : "Unable to simultaneously satisfy constraints"

ios - 支付宝 : payment gateway integration : (incorrectly calculated hash parameter)