I've been having a problem probably due to the new updated version of Swift. The problem is that this line of code keeps producing an error saying that:
"value of optional type '()?' not unwrapped; did you mean to use '!' or '?'?
cell?.hypeImageView?.image = UIImage(data: imageData)
This is the entire function:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell:HypeTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? HypeTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("HypeTableViewCell", owner: self, options: nil)[0] as? HypeTableViewCell
}
if let pfObject = object {
cell?.hypeNameLabel?.text = pfObject["name"] as? String
var votes:Int? = pfObject["votes"] as? Int
if votes == nil {
votes = 0
}
cell?.hypeVotesLabel?.text = "\(votes!) votes"
let credit:String? = pfObject["cc_by"] as? String //if prob change to var
if credit != nil {
cell?.hypeCreditLabel?.text = "\(credit!) / CC 2.0"
}
cell?.hypeImageView?.image = nil
if var urlString:String? = pfObject["url"] as? String {
var url:NSURL? = NSURL(string: urlString!)
if var url:NSURL? = NSURL(string: urlString!) {
var error:NSError?
var request:NSURLRequest = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5.0)
NSOperationQueue.mainQueue().cancelAllOperations()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
(response:NSURLResponse!, imageData:NSData!, error:NSError!) -> Void in
cell?.hypeImageView?.image = UIImage(data: imageData)
})
}
}
}
return cell
}
How can I fix this issue?
Assuming you're using Swift 2.0 change the signature for the completionHandler
to expect optionals rather than implicitly unwrapped optionals, then the problem will go away.
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response:NSURLResponse?, data:NSData?, error:NSError?) -> Void in
guard let imageData = data else {
assertionFailure("No data found")
return
}
cell?.hypeImageView?.image = UIImage(data: imageData)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments