I just started coding in swift and I am at the point that I can get a single value out of the JSON but I can't seem to get all the values out of it by looping trough the array. so my question is how do I get all the values out and view it as float or string.
here is my code:
let url = URL(string: "http://api.fixer.io/latest")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
do
{
//Array
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
//print(myJson)
for items in myJson [AnyObject] {
print(items)
}
//here is the single value part, it looks for the rates then it puts it in label.
if let rates = myJson["rates"] as? NSDictionary{
if let currency = rates["AUD"]{
print(currency);
self.label.text=String(describing: currency)
}
}
}
catch
{
}
}
}
}
task.resume()

for items in myJson [AnyObject]I would not expect that to compile. @aircraft's answer casts the JSON result to the correct type in the line that defines the myJSON constant. Aside from using anas!force cast, that is the better way to go. (I'd rewrite that as a guard statement that exits if the cast fails, since the format of data from a remote serve may change on you.)mutableContainersis completely meaningless in Swift.