Slide 43
Slide 43 text
struct Review {
init?(dictionary: [String : Any]) {
guard let name = dictionary["name"] as? String,
let category = dictionary["category"] as? String,
let city = dictionary["city"] as? String,
let price = dictionary["price"] as? Int,
let ratingCount = dictionary["numRatings"] as? Int,
let averageRating = dictionary["avgRating"] as? Float,
let photo = (dictionary["photo"] as?
String).flatMap(URL.init(string:)) else {
return nil
}
self.init(name: name,
category: category,
city: city,
price: price,
ratingCount: ratingCount,
averageRating: averageRating,
photo: photo)
}
// ...
}
// Usage
let myReview = Review(dictionary: document.data())
Without Codable
struct Review: Codable {
// ...
}
// Usage
let myReview = try document.data(as: Review.self)
With Codable