在以 NSURLConnection 开头的行中,我收到错误:“无法使用 @value NSURLRequest 类型的参数列表调用 init”
这可能与我的解析数据库中的错误有关吗?这是我第一次尝试使用解析(第一次尝试使用数据库进行编码,我正在遵循教程)
这是我的代码,其中我的函数给了我这个错误:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell:CatsTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CatsTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("CatsTableViewCell", owner: self, options: nil)[0] as? CatsTableViewCell
}
if let pfObject = object {
cell?.catNameLabel?.text = pfObject["name"] as? String
var votes:Int? = pfObject["votes"] as? Int
if votes == nil {
votes = 0
}
cell?.catVotesLabel?.text = "\(votes!) votes"
var credit:String? = pfObject["cc_by"] as? String
if credit != nil {
cell?.catCreditLabel?.text = "\(credit!) / CC 2.0"
}
cell?.catImageView?.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?.catImageView?.image = UIImage(data: imageData)
})
}
}
}
return cell }
最佳答案
NSURLConnection.sendAsynchronousRequest
的签名是
class func sendAsynchronousRequest(_ request: NSURLRequest,
queue queue: NSOperationQueue,
completionHandler handler: (NSURLResponse?,
NSData?,
NSError?) -> Void)
所有返回参数都是可选的。
顺便说一句:可选绑定(bind)行中的注释令人困惑。如果绑定(bind)成功,则该值是非可选的。例如,在大多数情况下使用 let
而不是 var
if let urlString = pfObject["url"] as? String {
if let url = NSURL(string: urlString) {
var error: NSError?
let request = NSURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5.0)
...
最后,不要在 cellForRowAtIndexPath
中调用异步任务。
关于swift - 无法使用 NSURLRequest 类型的参数列表调用 init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646932/