简体   繁体   English

如何从嵌套数组返回值-Swift 3

[英]how to return value from nested array - swift 3

My api query is returning several values in an array (as below), which I need to use for further functions. 我的api查询正在数组中返回多个值(如下所示),我需要将其用于其他功能。 How do I retrieve the value 60, which is nested? 如何获取嵌套的值60?

My code eg let uid = value["uid"] as? String 我的代码例如, let uid = value["uid"] as? String let uid = value["uid"] as? String works well to return uid and name. let uid = value["uid"] as? String可以很好地返回uid和name。 But for let rest = value["field_resting_bpm"] as? String 但是, let rest = value["field_resting_bpm"] as? String let rest = value["field_resting_bpm"] as? String it returns nil. let rest = value["field_resting_bpm"] as? String返回nil。

How do I correct this line to return the nested value '60'?: 如何更正此行以返回嵌套值“ 60” ?:

 let rest = value["field_resting_bpm"] as? String'

the api query is: api查询为:

    let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
    let base64Credentials = credentialData.base64EncodedStringWithOptions([])
    let headers = ["Authorization": "Basic \(base64Credentials)"]

var checkUserEndpoint: String = " https://example.site/ios1/user/1.json " var checkUserEndpoint:字符串=“ https://example.site/ios1/user/1.json

   Alamofire.request(.GET, checkUserEndpoint, parameters: nil, headers: headers, encoding: .JSON)
        .responseJSON { response in
            guard response.result.error == nil else {
                print("ran alamofire")
                //got an error in getting the data, need to handle it
                print("error calling GET")
                print(response.result.error!)
                return
            }

            guard let value = response.result.value else {
                print("no result data received when calling GET")
                return
            }
            // return UID
            print("ran alamofire")
            var datareturned = JSON(value)
            print("VALUE: \(value)")

it returns this in my console: 它在我的控制台中返回:

 uid = 1;
 name = "Guest User";
"field_resting_bpm" =     {
    und =         (
                    {
            value = 60;
        }
    );

UPDATE: this field is an Integer in Drupal user account settings. 更新:此字段是Drupal用户帐户设置中的整数。 I have added a text field (so it should return a string), named field_rest_bpm_string, also for testing 我添加了一个名为field_rest_bpm_string的文本字段(因此它应该返回一个字符串),也用于测试

the json on the site returns: 网站上的json返回:

"field_resting_bpm":{"und":[{"value":"60"}]},"field_rest_bpm_string":{"und":[{"value":"65","format":null,"safe_value":"65"}]} “ field_resting_bpm”:{“ und”:[{“ value”:“ 60”}]},“ field_rest_bpm_string”:{“ und”:[{“ value”:“ 65”,“ format”:null,“ safe_value” :“ 65”}]}

NOTE : Code is in Swift 3 but you can easily convert that in swift 2. SwiftyJSON version code is same in 2 and 3. 注意 :代码在Swift 3中,但是您可以轻松地在swift 2中进行转换SwiftyJSON版本代码在2和3中相同。

If you are using SwiftyJSON then things are pretty much simpler 如果您使用的是SwiftyJSON那么事情就简单得多

let value = datareturned["field_resting_bpm"]["und"][0]["value"].stringValue
print(value)

If you are not using SwiftyJSON 如果您不使用SwiftyJSON

let rest = value["field_resting_bpm"] as? NSDictionary
let und = (rest?.value(forKey: "und") as! NSArray).object(at: 0) as! NSDictionary
let yourValue = und.value(forKey: "value") as! String

Something like this. 这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM