遇到一个编译报错:

Escaping closure captures non-escaping parameter 'onCompletion'

代码如下:

func fetchRecentPhotos(completion: (PhotosResult) -> Void)
    {
        let url = recentPhotosURL()
        let request = URLRequest(url: url as URL)
        let task = session.dataTask(with: request)
        {
            (data, response, error) -> Void in
            
            let result = self.processRecentPhotosRequest(data: data as NSData?, error: error as NSError?)
            completion(result)
        }
        task.resume()
    }

 这是由于completion导致的,默认闭包completion是@nonescaping的,只需要声明成@escaping即可。

func fetchRecentPhotos(completion: @escaping (PhotosResult) -> Void)

相关文章:

  • 2021-08-10
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2022-01-27
  • 2022-12-23
猜你喜欢
  • 2021-12-22
  • 2022-12-23
  • 2021-11-11
  • 2021-07-15
  • 2021-06-04
  • 2021-06-24
  • 2022-12-23
相关资源
相似解决方案