0

I have a problem getting this if else block to work as I wanted to. I have received the data from an API and while when I printed it it says "nil", however, the program still crashed

 print(i["dogs"]!!["breed"])
                if (i["dogs"]!!["breed"]) == nil {
                    item5.append(00000)
                } else {
                    item5.append((i["dogs"]!!["breed"] as! Int))
                }

the print prints out nil however, it seems that the if else block didn't work as I intended

thanks for the help !

EDIT: maybe I should've explained more clearly on my question... I am having some trouble understanding the part of the if-else statement that supposedly should run the block if the condition is met ((i["dogs"]!!["breed"]) == nil in this case). So why didn't my block run ?

user3175707
  • 1,123
  • 3
  • 11
  • 14
  • 2
    Holy moly @ the forced unwrappers `!`. Have a look at [this excellent canonical Q&A covering optionals and safe ways to unwrap and work with these](http://stackoverflow.com/questions/32170456/). Note that I will vote to close this question as a duplicate of the linked one. – dfrib Jul 07 '16 at 07:18
  • If you don't want crashes, then you should avoid using the crash operator `!` – Hamish Jul 07 '16 at 07:22

0 Answers0