Good day to all. Please tell me how to display the value "foods1", "foods2" of the key "favFoods" from the array if there is no such key in one of the indices of the array

Here is my Json

{ "pets": [ { "name" : "Purrsloud", "species" : "Cat", "favFoods" : "foods1", "birthYear" : 2016, "photo" : "https://learnwebcode.imtqy.com/json-example/images/cat-2.jpg" }, { "name" : "Barksalot", "species" : "Dog", "birthYear" : 2008, "photo" : "https://learnwebcode.imtqy.com/json-example/images/dog-1.jpg" }, { "name" : "Meowsalot", "species" : "Cat", "favFoods" : "foods2", "birthYear" : 2012, "photo" : "https://learnwebcode.imtqy.com/json-example/images/cat-1.jpg" } ] } 

Here is my code

  if a1.pets != nil { cell.l3.text = self.a1.pets![indexPath.row].favFoods } 

Here is the error that knocks out:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Thank you in advance

  • How to parse json? - VAndrJ
  • Thank you already figured out. Very pleased to have responded :) - swift

0