popctrl popctrl - 1 year ago 83
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 (

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

I've got an
, returned by
. 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
, 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
is a collection?

Answer Source

You can try like this

if object is NSDictionary {

} else if object is NSArray {


Hope this will solve your problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download