popctrl popctrl - 2 months ago 5
Swift Question

In Swift, how do I check if an object is a collection of any type?

I'm working on creating a JSON visualizer (

UIScrollView
with expandable views for each value) and I'm running into an issue:

I've got an
NSDictionary
, returned by
NSJSONSerialization.JSONObjectWithData
. I want to check the values of it's objects and find out which ones are Arrays or Dictionaries.

Ideally, I'm hoping that Arrays and Dictionaries have an underlying type, such as
Collection
or
Iterable
, so that I don't have to handle them separately. If they don't have that underlying type, how can I check to see if a given
NSDictionary.objectForKey
is a collection?

Answer

You can try like this

if object is NSDictionary {

} else if object is NSArray {

}

Hope this will solve your problem

Comments