Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3: Iterate through a _NSSingleObjectArrayI

Tags:

swift

I'm fetching data from a weather API. I'm not sure how to access the description?

"weather": <__NSSingleObjectArrayI 0x608000012910>(
{
    description = "overcast clouds";
    icon = 04n;
    id = 804;
    main = Clouds;
}
)

I tried:

print(weatherDict["weather"]!.description!)

It just gave me this:

(
    {
    description = "overcast clouds";
    icon = 04n;
    id = 804;
    main = Clouds;
  }
)

How do I properly access the description?

like image 896
user298519 Avatar asked Nov 27 '16 07:11

user298519


1 Answers

  • weather contains an array of dictionaries.
  • description is a key in the first item of the array.

The code unwraps weather safely and checks if the array is not empty:

if let weatherArray = weatherDict["weather"] as? [[String:Any]], 
   let weather = weatherArray.first {
       print(weather["description"]) // the value is an optional.
}
like image 192
vadian Avatar answered Oct 28 '22 00:10

vadian