我在使用Codable协议将2个协议整合在一起时遇到了一些麻烦。
我的理解是,即使我使用了自定义对象,只要该对象成功符合Codable,那么任何引用它的对象也可以符合。
我有以下代码,无法理解我收到错误的原因:
import UIKit
import Foundation
protocol PromotionProtocol: Codable {
var promotionType: Int? { get }
var promotionCode: String? { get }
var fileName: String? { get }
var facetDescription: String? { get }
var promoDescription: String? { get }
}
class Promotion: PromotionProtocol {
var promotionType: Int?
var promotionCode: String?
var fileName: String?
var facetDescription: String?
var promoDescription: String?
}
protocol PromotionInfoProtocol: Codable {
var cornerPromotion: PromotionProtocol? { get }
var mainPromotion: PromotionProtocol? { get }
var isTopFive: Bool? { get }
}
class PromoInfo: PromotionInfoProtocol {
var cornerPromotion: PromotionProtocol?
var mainPromotion: PromotionProtocol?
var isTopFive: Bool?
}
'PromotionProtocol'中的所有内容都是String或Int,所以没关系。但是我得到了Class Promotion不符合Encodable和Decodable的错误。