Rorschach Rorschach - 5 months ago 17
Swift Question

Check if JSON as dictionary contains Optional(null)

I want to make an if statement that checks if my JSON (item_section) has a null value at a certain key.

This code:

let my_dict = item_section[counter].dictionaryValue
print(my_dict["creator"])


prints:
Optional(null)
when the creator is not set.

If it is set if it prints the creator's name, like:
user_729834892
.

How can I make an if statement that checks if the creator is null?

Such as:

if ((my_dict["creator"]) == nil) { //This line does not work
my_dict["creator"] = "No Maker"
}

Answer

Try

if my_dict["creator"].type == .Null {
    print("creator is null")
}