【问题标题】:Swift error AFNetworkingSwift 错误 AFNetworking
【发布时间】:2014-11-27 13:28:42
【问题描述】:

我在运行时有一个错误,而不是如何修复它。我有以下代码使用 AFNetworking 从 webservice 获取数据。数据收集在 responseObject 中。我把代码放在这里:

import Foundation
import UIkit

// Protocolo a implementar por la clase que delegue esta
protocol WebServiceProtocolo {
    // funcion que implementará la clase delegada y que recibirá los datos de repuesta a la llamada
    func didReceiveResponse(respuesta : NSDictionary)
}

// PRUEBA DE CONEXIÓN CON WEBSERVICE A TRAVES DE AFNETWORKING
class webServiceCallAPI : NSObject {

    var delegate : WebServiceProtocolo?

    let manager : AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()
    var responseObject : AnyObject?

    // Esta llamada devuelve una lista con los vendedores del sistema
    func obtenerVendedores()  {

       // var parametros = ["user":"b17e70697e2374","password":"3eaf2e91"]
       // var jsonDict : NSDictionary
        var jsonArray : NSArray!
        var error : NSError?
        manager.GET("http://losbarkitos.herokuapp.com/vendedores/", parameters: nil,
            success: {(operation: AFHTTPRequestOperation!, responseObject) in
                println("responseObject: \(responseObject)")
                var jsonDict = responseObject as NSDictionary
                self.delegate?.didReceiveResponse(jsonDict)},

            failure: {(operation: AFHTTPRequestOperation!, error: NSError!) in
                println("Error: \(error.localizedDescription))")})
      //  self.delegate?.didReceiveResponse(responseObject as NSDictionary)

    }
}

这是“responseObject”的内容:

响应对象:

 (
        {
        codigo = 1;
        nombre = Celia;
    },
        {
        codigo = 2;
        nombre = Mikel;
    },
        {
        codigo = 3;
        nombre = Rafaela;
    },
        {
        codigo = 4;
        nombre = Miguel;
    }
)

错误在以下行:

  ---  var jsonDict = responseObject as NSDictionary  ---> Thread 1:EXC_BAD_ACCESS (Code 2, address...)

有人知道怎么回事吗?

谢谢

【问题讨论】:

    标签: swift parse-platform afnetworking


    【解决方案1】:

    显然,您的响应对象不能强制转换 (as) 到 NSDictionary。可能是其他类型,尝试转换为 NSArray。顺便说一句,您应该始终使用 as? 进行转换,并检查结果是否为零,这将有助于避免此类运行时崩溃

    【讨论】:

      【解决方案2】:

      您的 responseObject 是数组(由括号括起来的所有内容表示),而不是字典。它实际上是一个由四个字典对象组成的数组。因此,尝试以字典的形式访问该数组本身将失败。

      顺便说一句,如果您使用if let 语法,您可以更优雅地捕获这些错误:

      if let jsonArray = responseObject as? NSArray {
          for dictionary in jsonArray {
              // do something with the individual dictionary objects
          }
      } else {
          println("not an array!")
      }
      

      以上使用NSArray。或者,您也可以使用 Swift 字典的 Swift 数组:

      if let jsonArray = responseObject as? [[String : AnyObject]] {
          for dictionary in jsonArray {
              // do something with the individual dictionary objects
          }
      } else {
          println("not an array!")
      }
      

      【讨论】:

      • 谢谢。但我不明白如果我直接从 web 服务捕获,为什么“responseObject”是“NSArray”。网络服务是在 django 中完成的,答案是通过“HttpResponse”,这会返回一个字典
      • 我只能说您记录的responseObject 不是字典。它是一个包含四个字典对象的数组,每个对象都有一个codigonombre 键。你说它应该是一本字典,但我无法将其与你与我们分享的内容相协调。 (a) 您对响应的预期不正确;或 (b) 服务器代码(或您的请求是如何形成的)存在问题,导致返回的对象结构类型错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2018-03-09
      • 2012-12-11
      • 2012-09-10
      • 2014-08-28
      • 2017-10-27
      相关资源
      最近更新 更多