Reputation: 1868
I am using Swift 3.
I am getting the following response from a server and i need to parse and get values such as, Account No, Account Type, Address etc.
parsedData: (
{
key = "<null>";
offset = 1;
partition = 0;
topic = test;
value = {
"Account No" = 675;
"Account Type" = Saving;
Address = location;
User ID = 601;
User Name = Stella;
};
}
)
I have been trying to get value first, and then planning to get each value,
var temp: NSArray = parsedData["value"] as! NSArray
but this is giving error as cannot convert value of type String to expected argument type Int.'
How to retrieve values from the above mentioned array?
Upvotes: 0
Views: 438
Reputation: 2547
Try this
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")
Upvotes: 0
Reputation: 100503
You parse a dictionary as an array
var temp: NSDictionary = parsedData["value"] as? NSDictionary
Upvotes: 0
Reputation: 8504
parsedData
is an array
which contains Dictionary
at first index.
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**
//////SECOND APPROACH IF YOU HAVE DICTIONARY IN RESPONSE
var valueDictionary : NSDictionary = parsedData["value"] as? NSDictionary
let accountNumber = valueDictionary ["Account No"] //**Account number**
Upvotes: 1