这是代码使用不知道为什么这个错误发生,即使我不能使用弱自我与struct。想要存储在selectedicon URL上存在的blackWhite图像:
struct ServiceViewModel {
private var service: Service
var selectedicon: URL?
var name: String
var subServiceCount: Int {
return service.subServices.count
}
var subServicesViewModel = [SubServiceViewModel]()
var blackWhiteImage: UIImage?
var isSelected: Bool = false
init(service: Service) {
self.service = service
self.selectedicon = URL(string: service.icon)
self.name = service.name
}
mutating func generateBlackAndWhiteImage() {
guard let url = selectedicon else {
return
}
KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil) { (image, error, _, _) in
//self.blackWhiteImage = image?.convertBlackAndWhite()
self.blackWhiteImage = #imageLiteral(resourceName: "IM.png")
}
}
}
答案 0 :(得分:1)
let `self` = self
// or
let `self` = UnsafeMutablePointer(&self)
KingfisherManager.shared.retrieveImage(with: url, options: nil, progressBlock: nil) {
(image, error, _, _) in
//self.blackWhiteImage = image?.convertBlackAndWhite()
self.blackWhiteImage = #imageLiteral(resourceName: "IM.png")
// or
self.pointee.blackWhiteImage = image?.convertBlackAndWhite()
}