Ios Swift可选类型未展开
我一直有一个问题,可能是由于新的更新版本的Swift。问题是这行代码不断产生一个错误,表示: 可选类型“?”的值未展开;你想用“!”吗还是“?” 这是整个功能:Ios Swift可选类型未展开,ios,swift,Ios,Swift,我一直有一个问题,可能是由于新的更新版本的Swift。问题是这行代码不断产生一个错误,表示: 可选类型“?”的值未展开;你想用“!”吗还是“?” 这是整个功能: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? { var cell:HypeTableView
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
}
如何解决此问题?假设您使用的是Swift 2.0,将completionHandler的签名更改为期望选项,而不是隐式打开选项,则问题将消失
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)
}
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)
}