Skip to content

Issue with subscript not existing causing crash #65

@jhoughjr

Description

@jhoughjr

Ive stumbled upon a case where my server returned a partially formed body that doesnt include a key.
I see no way in the api to safely access a subscript that may not exist.
Json line 65: json.jsonData = (jsonData as! [String: Any])[s]!

My code:

 .onJson { j in
            print(j)
            if j.stringified?.contains("script") != nil { // added to get around the crash
            $script.wrappedValue = j["script"].string
            } // added
        }

Id expect nil as opposed to a crash. I think thats what js would do, just return undefined for a non existing key instead of treating it as an error.
In my xp, though I use force unwraps , I nearly always end up reverting them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions