Typealias is like a generic but for a protocol. Warning, when you create the method of your protocol because you can't change the name of your parameter as you made.
If your name as a parameter is model you have to keep this name and don't call it user, otherwise your typealias is useless and the method is not conforming at your protocol.
I have a little different result because I'm more advanced in the course.
protocol ProducesCardViewModel {
associatedtype ModelData
associatedtype Advertiser
static func modelToViewModel(model: ModelData) -> CardViewModel
static func advertiserToViewModel(adv: Advertiser) -> CardViewModel
}
class CardViewModel: ProducesCardViewModel {
let imageUrls: [String]
let attributedString: NSAttributedString
let textAlignment: NSTextAlignment
static func modelToViewModel(model: User) -> CardViewModel {
let ageString = model.age != nil ? "\(model.age!)" : "N\\A"
let professionString = model.profession == "" ? "Not available" : "\(model.profession!)"
let attributedText = NSMutableAttributedString(string: model.name ?? "", attributes: [.font: UIFont.systemFont(ofSize: 32, weight: .heavy)])
attributedText.append(NSAttributedString(string: " \(ageString)", attributes: [.font: UIFont.systemFont(ofSize: 24, weight: .regular)]))
attributedText.append(NSAttributedString(string: "\n\(professionString)", attributes: [.font: UIFont.systemFont(ofSize: 20, weight: .regular)]))
let imageUrls = [model.imageUrl1, model.imageUrl2, model.imageUrl3].compactMap { $0 }.filter { $0.isEmpty == false }
return CardViewModel(imageNames: imageUrls, attributedString: attributedText, textAlignment: .left)
}
static func advertiserToViewModel(adv: Advertiser) -> CardViewModel {
let attributedText = NSMutableAttributedString(string: adv.title, attributes: [.font: UIFont.systemFont(ofSize: 34, weight: .heavy)])
attributedText.append(NSAttributedString(string: "\n\(adv.brandName)", attributes: [.font: UIFont.systemFont(ofSize: 24, weight: .bold)]))
return CardViewModel(imageNames: [adv.posterPhotoName], attributedString: attributedText, textAlignment: .center)
}
}