Swift 3: Only continue processing Dictionary value if it's of type Array -


i receiving data structure on wire that's of type [string: anyobject]. reason anyobject because value can of type array or dictionary. condition straight forward:

if let data = list["foo"], data.count > 0 {   // stuff } 

my problem is, want condition pass if data array. condition have fails because count property seems work on dictionary (it counts number of keys in dictionary).

what's best way handle this?

you can cast data array in if statement:

if let data = list["foo"] as? [any], data.count > 0 {   // stuff } 

this make sure data array before doing operations on it.


Comments