Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用AFN时如何从任何对象获取值_Ios_Swift_Afnetworking - Fatal编程技术网

Ios 使用AFN时如何从任何对象获取值

Ios 使用AFN时如何从任何对象获取值,ios,swift,afnetworking,Ios,Swift,Afnetworking,我在与swift的新个人项目中使用了AFNetworking。当我向服务器发送登录请求时,服务器将使用json返回响应,而AFNetworking将json转换为Anyobject。但是当我尝试使用anyobject时,我遇到了一些问题 以下是登录成功时的json数据: {"code":0,"data":{"id":"1"}} 这是我的登录码: manager.POST("\(SERVER_HOST)User/login", parameters: params, success: { (op

我在与swift的新个人项目中使用了AFNetworking。当我向服务器发送登录请求时,服务器将使用json返回响应,而AFNetworking将json转换为Anyobject。但是当我尝试使用anyobject时,我遇到了一些问题

以下是登录成功时的json数据:

{"code":0,"data":{"id":"1"}}
这是我的登录码:

manager.POST("\(SERVER_HOST)User/login", parameters: params, success: { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in
      var code = response.objectForKey("code") as Int

      if code == 0{
         var data = response.objectForKey("data") as NSDictionary
         var id = data.objectForKey("id")?.integerValue

         self.performSegueWithIdentifier("loginSucceed", sender: self)
      }

})
所以我的问题是:代码可以工作,但是当我使用

var id = data.objectForKey("id") as Int
就像我得到代码值的方式一样,应用程序崩溃了,我得到了一个零值。为什么

另一个问题是:用更复杂的json字符串获取值的正确方法是什么。
非常感谢您的帮助

您的代码在执行以下操作时崩溃:

var id = data.objectForKey("id") as Int
因为与
“id”
相对应的值是
字符串
“1”
,而不是
Int
1
。如果您对类型有错误,强制强制强制转换将崩溃。将条件强制转换
用作?
并将其与可选绑定结合使用更安全:

if let code = response["code"] as? Int {
    // if we get here we know "code" is a valid key in the response dictionary
    // and we know we got the type right.  "code" is now an unwrapped `Int` and
    // is ready to use.

    if code == 0 {
        if let data = response["data"] as? NSDictionary {
            // if we get here, we know "data" is a valid key in the response
            // dictionary and we know it holds an NSDictionary.  If it were
            // some other type like `Int` we wouldn't have entered this block

            if let id = data["id"] as? String {
                // if we get here, we know "id" is a valid key, and its type
                // is String.

                // Use "toInt()" to convert the value to an `Int` and use the
                // default value of "0" if the conversion fails for some reason
                let idval = id.toInt() ?? 0
                println("idval = \(idval)")
            }
        }
    }
}
这个代码可能有用

let json = JSONValue(response)
var code: Int  = json["code"].integer!

if code == 0 {
    if let data = json["data"].object {
       var id = json["data"]["id"].integer!
    }
    self.performSegueWithIdentifier("loginSucceed", sender: self)
}

在github上搜索了几个小时后,我终于在swift中找到了一个解析json的非常有用的工具

任何看到这个问题的人都可以在一些个人项目中使用AlamoFire而不是AFNetworking

这里是另一个链接,可以同时使用AlamoFire和SwiftyJSON


这对代码中的注释非常有帮助!但是我怎么知道值的类型呢?为什么代码的类型是Int而id的类型是String?通常在编写解析代码之前,您只需获取JSON数据并将其打印到日志中。请注意,“1”在引号中,而0不是。此外,如果您不确定它是什么类型,请尝试依次将其转换为两种类型<代码>如果let id=Data[“id”]as?Int{//process Int}如果让id=data[“id”]作为?字符串{//process String}
谢谢,我会按照你说的那样尝试如何定义
JSONValue