Search code examples
arraysswiftswift4bundlensbundle

Trying do deal with an object that can be an array or a number


I am trying to read the value of UIDeviceFamily from the infoDictionary, like this:

if let family = Bundle.main.infoDictionary?["UIDeviceFamily"] {
  if family is Array<Int> {

  }
}

According to a 2010 documentation, UIDeviceFamily can be a NSNumber or an array of NSNumber.

Thanks to non existing documentation, I guess that, in Swift, it would be a Int or Array<Int>.

When I run this code, I get the second if to be true. So, in my case, family is an Array<Int>.

This is what I do not understand. If family is an Array<Int>, the next line after the second if could be

let firstValue = family.first

but that will fail.

How do I extract the values from family when it is a an array or a number? Is there an easy way to do that?


Solution

  • Try

    if let arr =  family as? Array<Int> {
       print(arr)
    }
    else if let item = family as? Int {
       print(item)
    }