gpt4 book ai didi

ios - Swift 可选类型未解包

转载 作者:行者123 更新时间:2023-11-30 13:57:54 25 4
gpt4 key购买 nike

我遇到了一个问题,可能是由于新更新的 Swift 版本所致。问题是这行代码不断产生错误:

"value of optional type '()?' not unwrapped; did you mean to use '!' or '?'?

cell?.hypeImageView?.image = UIImage(data: imageData)

这是整个函数:

    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)
}

关于ios - Swift 可选类型未解包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com