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

问题描述

我在我的 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
            ...
    });

错误是:

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

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?

推荐答案

在斯威夫特编译器编译这些ObjC标题变为斯威夫特导致命名冲突:

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

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.

作为解决方法,可以设置选项参数或 o1 或者 o2 可以暂时重命名,直到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.

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

08-04 14:45