Я пытаюсь изменить свой код с Swift 1.2 на Swift 2.0, но у меня возникли проблемы с «sendAsynchronousRequest», потому что он всегда показывает предупреждение, потому что он устарел. Я попытался использовать другое решение из этого сообщения: Cannot invoke 'sendAsynchronousRequest' in Swift 2 with an argument list, но он все еще не работает, и я снова получаю такое же предупреждение.sendAsynchronousRequest устарел в iOS 9
Что мне нужно изменить в своем коде для решения этого предупреждения? Предупреждение заключается в следующем:
sendAsynchronousRequest осуждался в прошивке 9, используйте dataTaskWithRequest: completionHandler
Это мой код:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// try to reuse cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MarcaCollectionViewCell
// get the deal image
let currentImage = marcas[indexPath.row].imagen
let unwrappedImage = currentImage
var image = self.imageCache[unwrappedImage]
let imageUrl = NSURL(string: marcas[indexPath.row].imagen)
// reset reused cell image to placeholder
cell.marcaImageView.image = UIImage(named: "")
// async image
if image == nil {
let request: NSURLRequest = NSURLRequest(URL: imageUrl!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse?,data: NSData?,error: NSError?) -> Void in
if error == nil {
image = UIImage(data: data!)
self.imageCache[unwrappedImage] = image
dispatch_async(dispatch_get_main_queue(), {
cell.marcaImageView.image = image
})
}
else {
}
})
}
else {
cell.marcaImageView.image = image
}
return cell
}
Итак, вы хотите, чтобы подавить предупреждение или вы хотите, чтобы перейти к 'dataTaskWithRequest: completeHandler'? – Wain
Привет @Wain, я хотел бы сделать все правильно, поэтому я предпочитаю подавлять предупреждение, если это правильно. Заранее спасибо. –
Подавление предупреждения об устаревших методах - не очень хорошая идея. Просто перейдите к использованию NSURLSession, как предупреждает вас. – Abizern