'闭包不能隐含地捕获变异的自我参数'结构中的错误

时间:2017-11-01 12:03:05

标签: ios kingfisher swift3.2

enter image description here

这是代码使用不知道为什么这个错误发生,即使我不能使用弱自我与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")
    }
  }


}

1 个答案:

答案 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()
  }