I try to parse and assign data, which I am becoming from Firebase The structure in Firebase looks like this:
I try to fetch data from database and assign it to instance of class Meal:
ref = Database.database().reference()
databaseHandle = ref.child("Meal").observe(.value, with: { (snapshot) in
var downloadedName : String!
var downloadedPhoto : String!
var downloadedRating : Int!
var downloadedSteps : Array <String>!
var downloadedIngredients : [Dictionary <String, String>]!
print(snapshot.value)
if let dict = snapshot.value as? Dictionary<String, Any>{
print("VALUES!!!")
for key in dict.keys {
if let values = dict[key] as? Dictionary<String, Any> {
print(values)
if let name = values["name"] as? String{
downloadedName = name
}
if let photo = values["photo"] as? String{
downloadedPhoto = photo
}
if let rating = values["rating"] as? Int{
downloadedRating = rating
}
if let steps = values["steps"] as? Array<String>{
downloadedSteps = steps
}
if let ingredients = values["ingredients"] as? [Dictionary <String, String>]{
downloadedIngredients = ingredients
}
let meal = Meal(name: downloadedName, photo: UIImage(named: downloadedPhoto), rating: downloadedRating, steps: downloadedSteps, ingredients: downloadedIngredients)
self.meals.append(meal!);
}
}
The Meal class itself looks like this:
class Meal {
var name: String
var photo: UIImage?
var rating: Int
var steps: Array<String>
var ingredients: [Dictionary<String, String>]}
I get the first print - the whole data, so the connection with DB is OK, but as i try to assign it - nothing happens, no errors, no data (the second print with message VALUES!!! is not shown at all, what am I doing wrong?
Here is also what I get by first print
Optional(<__NSArrayM 0x600002fdaa60>(
{
ingredients = (
{
amount = 100;
ingredient = milk;
measurement = ml;
},
{
amount = 120;
ingredient = milk;
measurement = ml;
}
);
name = "Caprese Salad";
photo = meal1;
rating = 4;
steps = (
test1,
test11
);
},
{
ingredients = (
{
amount = 100;
ingredient = milk;
measurement = ml;
},
{
amount = 120;
ingredient = milk;
measurement = ml;
}
);
name = "Chicken and Potatoes";
photo = meal2;
rating = 3;
steps = (
test2,
test22
);
},
{
ingredients = (
{
amount = 100;
ingredient = milk;
measurement = ml;
},
{
amount = 120;
ingredient = milk;
measurement = ml;
}
);
name = "Pasta with Meatballs";
photo = meal3;
rating = 2;
steps = (
test3,
test33
);
}
)
)
So, I assume, I retrieve the data in the false way at some point, how could i fix it?

if let dict = snapshot.value as? Dictionary<String, Any>{}, so ifsnapshot.valueis not a Dictionary where keys areStringand values are "whatever", you won't pass that test. Alright? Print of "values": starts withOptional(<__NSArrayM 0x600002fdaa60>(. Yeah, clearly that's an Array, not a Dictionary. So because of ` as? Dictionary<String, Any>`, you don't get your second print.snapshot.value. So instead of trying to parse it as it was a Dictionary, parse it as it is an Array.!), and the var declaration before the loop. Because currently if you missed anif let, you'd get the previous value, not a "default one". But you might want to use Codable (Swift 4+). Edit: Replacevalues["someString"]withaValue["someString"], small copy/paste mistake.