我在语法上无法从闭包内调用共享实例方法:
这是我的代码:
func getContactImage (contact:Contacts, completion:@escaping (_ myimage: UIImage)->()){//open 1 method
var animg = UIImage(named:"default.png")!
let surl = "https://~/contactimage.png"
Utilities.shared.downloadImage(surl: surl as NSString, completion: image as UIImage ->Void in animg = img)
completion(animg)
}
Utilities.shared.downloadImage行给出了一些错误,包括:
强制转换为'UIAccessibilityTraits'类型的值(aka'UInt64')不能转换为'UIImage'类型
共享实例方法如下:
@objc func downloadImage(surl: NSString, completion : @escaping (UIImage) -> Void ) {
//download image
}
从闭包内部调用共享实例方法的正确语法是什么?
答案 0 :(得分:0)
使用
Utilities.shared.downloadImage(surl) { (img) in
// use img here
}
还可以更改功能
@objc func downloadImage(_ surl: String, completion : @escaping (UIImage) -> Void ) { }
func getContactImage (contact:Contacts, completion:@escaping (_ myimage: UIImage)->()){//open 1 method
var animg = UIImage(named:"default.png")!
let surl = "https://~/contactimage.png"
Utilities.shared.downloadImage(surl) { (img) in
completion(img)
}
}
BTW鼓励使用SDWebImage