Swift 3中嵌套数组的值

时间:2018-01-23 07:14:44

标签: ios swift

我正在使用Swift 3。

我从服务器收到以下响应,我需要解析并获取诸如帐号,帐号类型,地址等值。

parsedData: (
        {
        key = "<null>";
        offset = 1;
        partition = 0;
        topic = test;
        value =         {
            "Account No" = 675;
            "Account Type" = Saving;
            Address = location;
            User ID = 601;
            User Name = Stella;
        };
    }
)

我一直在努力获得价值,然后计划获得每个价值,

var temp: NSArray = parsedData["value"] as! NSArray

但这是错误cannot convert value of type String to expected argument type Int.'

如何从上面提到的数组中检索值?

3 个答案:

答案 0 :(得分:1)

parsedDataarray,在第一个索引处包含Dictionary

let dicData = parsedData[0] as! Dictionary

let valueDictionary = dicData["value"] as! Dictionary  //dicData  also contains dictionary for key `value`


let accountNumber = valueDictionary ["Account No"] //**Account number**

//////第二种方法,如果你有反应中的字典

var valueDictionary : NSDictionary = parsedData["value"] as? NSDictionary 
    let accountNumber = valueDictionary ["Account No"] //**Account number**

答案 1 :(得分:0)

您将字典解析为数组

var temp: NSDictionary = parsedData["value"] as? NSDictionary 

答案 2 :(得分:0)

试试这个

let dicData = parsedData[0] as! Dictionary
var temp: NSDictionary = dicData["value"] as? NSDictionary
let accountNumber = temp.object(forKey: "Account No")
let accountType = temp.object(forKey: "Account Type")
let address = temp.object(forKey: "Address")
let userID = temp.object(forKey: "User ID")
let userName = temp.object(forKey: "User Name")