调用Swift Extra参数

时间:2015-02-10 17:07:17

标签: swift compiler-errors

我在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 +点击它时,XCode甚至不识别该方法:“找不到符号”。

我怀疑它对块有一些东西,因为我可以调用同一类的其他方法,这几乎是同一个类,但没有完成块。工作示例:

self.lastLookImageView.setImageWithURL(NSURL(string: self.lastLook!.image), placeholderImage: nil, options: SDWebImageRefreshCached) 

出于上下文的缘故:我在我的混合Obj-C和Swift项目中使用了一个名为SDWebImage(https://github.com/rs/SDWebImage)的组件。该类已经在我的Bridging-Header.h上导入

1 个答案:

答案 0 :(得分:1)

Swift方法签名应为:

func setImageWithURL(url: NSURL!, forState state: UIControlState,
            placeholderImage placeholder: UIImage! options: SDWebImageOptions)

所以你需要用:

来调用它
self.lastLookImageView.setImageWithURL(url, forState: .Normal, 
            placeholderImage: nil, options: SDWebImageRefreshCached)

此方法没有完成处理程序,因此请将其保留。