'sd_setImage(with:placeholderImage:completed:)' 与 Swift 3 的模糊使用 [英] Ambiguous use of 'sd_setImage(with:placeholderImage:completed:)' with Swift 3

查看:30
本文介绍了'sd_setImage(with:placeholderImage:completed:)' 与 Swift 3 的模糊使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 imageView 上使用 SDWebImage 进行以下调用,它在 Swift 2 中工作正常,但在 中出现错误XCode 8 beta 5 使用 Swift 3 编译:

I am making the following call using SDWebImage on my imageView, which works fine with Swift 2 but gives an error with XCode 8 beta 5 compiling with Swift 3:

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

错误是:

'sd_setImage(with:placeholderImage:completed:)'的歧义使用

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

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

I suspect I have something wrong in the signature for the completed handler, but I can't figure out what the syntax should be. What am I missing?

推荐答案

Swift 编译器将 ObjC 头文件转换为 Swift,从而导致命名冲突:

The Swift compiler translates the ObjC headers into Swift which leads to naming collisions:

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 参数.

Their only difference is the additional optionsparameter in o2.

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 参数作为可选参数.

Because options was translated into an optional parameter (default assigned an empty array) calling s1 in Swift leads to an ambiguous use. Calling s2 could simply have the same implementation. When providing such methods in Swift code one would add the options parameter as optional in a single function implementation.

作为一种变通方法,可以设置 options 参数或暂时重命名 o1o2,直到 SDWebImage 被翻译成 Swift.

As a workaround the options parameter could be set or o1 or o2 could be renamed temporarily until SDWebImage will be translated into Swift.

这篇关于'sd_setImage(with:placeholderImage:completed:)' 与 Swift 3 的模糊使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆