我正在尝试将我的代码从Swift 1.2更改为Swift 2.0,但我遇到了一些关于"sendAsynchronousRequest"的问题,因为它总是显示警告,因为它已被弃用.我尝试过使用此帖子中的其他解决方案:不能在Swift 2中使用参数列表调用'sendAsynchronousRequest'但它仍然无法工作,我再次收到相同的警告.
我必须在代码中更改以解决此警告?警告如下:
在iOS 9中不推荐使用sendAsynchronousRequest,使用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
}
Run Code Online (Sandbox Code Playgroud)
Kel*_*ler 10
警告警告,NSURLConnection已经死亡.NSURLSession万岁.
let session = NSURLSession.sharedSession()
let urlString = "https://api.yoursecureapiservergoeshere.com/1/whatever"
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
print("done, error: \(error)")
}
dataTask.resume()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4989 次 |
| 最近记录: |