You code is muddling two distinct things: initialization of an object with a set of values for its members, and extracting those members from a dictionary. Just write two seperate initializers:
import Foundation
struct User {
let uid: String
let username: String
/*
// Due to the absence of an explicit initializer declaration,
// the compiler will synthesize an implicit member-wise initailizer like this:
init(uid: String, username: String) {
self.uid = uid
self.username = username
}
*/
}
extension User {
// Putting this initializer in an extension preserves he member-wise intializer
init?(fromDict dict: [String: Any]) {
guard let uid = dict["uid"] as? String,
let username = dict["username"] as? String
else { return nil }
self.init(uid: uid, username: username)
}
}
struct Offer {
let user: User
let caption: String
let imageURL: String
let creationDate: Date
/*
// Due to the absence of an explicit initializer declaration,
// the compiler will synthesize an implicit member-wise initailizer like this:
init(
user: User,
caption: String,
imageURL: String,
creationDate: Date
) {
self.user = user
self.caption = caption
self.imageURL = imageURL
self.creationDate = creationDate
}
*/
}
extension Offer {
// Putting this initializer in an extension preserves he member-wise intializer
init?(fromDict dict: [String: Any]) {
guard let user = dict["user"] as? User,
let caption = dict["caption"] as? String,
let imageURL = dict["image_url"] as? String,
let secondsFrom1970 = dict["creation_date"] as? Double
else { return nil }
self.init(
user: user,
caption: caption,
imageURL: imageURL,
creationDate: Date(timeIntervalSince1970: secondsFrom1970)
)
}
}
Some notes:
- Using the nil-coalescing operator (
??) to provide non-sensical default values in the case of nil is really bad practice. It hides failures and silently introduces data integrity issues; don't do it.
String is not an appropriate type for a member called imageURL. Use URL.
- If these dicts are coming from JSON, use the
Codable protocol to automate all of this boilerplate code.
String is a poor type for an ID, primarily because it's really slow, in comparison to more suitable types like UUID and Int. This is particularly true in most databases, where textual comparison is much slower than Int/UUID comparison.