swift - 无法使用 NSURLRequest 类型的参数列表调用 init

标签 swift parse-platform nsurl

在以 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/

相关文章:

ios ARC 强制保留 NSURL

iOS 10 自动布局 - viewDidLayoutSubviews 重复调用

java - 从 Parse 下载文件抛出 NullPointerException

ios - 从服务器加载的 CollectionView/TableView 中的图像存在问题,不会立即加载到每个单元格中

iOS 11 使用 ATS 从 HTTP 加载图像

ios - 随机获取 Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.”

swift - 如何快速组合多个类型删除模块?

swift - 进度条不异步变化

swift - 在 Swift 中,如何将 CFStringRef[] 和 CFTypeRef[] 转换为 CMutablePointer< COpaquePointer >?

Xcode:无法构建 ParseFacebookUtilsV4 (Cocoapods)