我在调用 Swift 项目中的特定方法时遇到问题。这是 ObjC 方法签名:
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
我无法让它工作,因为 XCode 不断告诉我“调用中存在额外参数‘placeholderImage’”
self.lastLookImageView.setImageWithURL(url,
placeholderImage: nil,
options: SDWebImageRefreshCached,
completed: {
(image, error, cacheType) -> Void in
image = image.blurredImageWithRadius(30, iterations: 2, tintColor: UIColor.clearColor())
}
)
当我 Cmd+Click 时,XCode 甚至无法识别该方法:“找不到符号”。
我怀疑它与该 block 有关,因为我可以调用同一类的其他方法,这几乎是相同的,但没有完成 block 。工作示例:
self.lastLookImageView.setImageWithURL(NSURL(string: self.lastLook!.image), placeholderImage: nil, options: SDWebImageRefreshCached)
就上下文而言:我在 Obj-C 和 Swift 混合项目中使用了一个名为 SDWebImage ( https://github.com/rs/SDWebImage ) 的组件。该类已导入到我的 Bridging-Header.h
最佳答案
Swift 方法签名应该是:
func setImageWithURL(url: NSURL!, forState state: UIControlState,
placeholderImage placeholder: UIImage! options: SDWebImageOptions)
所以你需要这样调用它:
self.lastLookImageView.setImageWithURL(url, forState: .Normal,
placeholderImage: nil, options: SDWebImageRefreshCached)
此方法没有完成处理程序,因此请将其保留。
关于Swift 调用中的额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28437595/