Search code examples
swiftanyobject

Swift check if array of anyObject have value


I have this array of anyObject that i use to read my JSON data downloaded.

datiArticolo!["anagrafica"]![linguaApp!]!!["img"]!!["path"]

The problem is that not always i have che ["img"] or/and ["path"] values. how can i check if there are and if not do another action?


Solution

  • You generally want to check all forced unwraps to prevent crashes so you would continue this pattern up the chain. See more about optional chaining.

    if let img = datiArticolo!["anagrafica"]![linguaApp!]!!["img"], path = img["path"] {
     // do something with `path`
    }