尝试创建可存储不同类型类别的目录。
尝试使用泛型和/或AnyObject,但在访问对象时遇到问题
Class Offer {
var name: Sring = ""
}
class Promotion {
var name: String = ""
}
class Category<T> {
var categoryName: String = ""
var list: [T]?
}
class Catalog {
var categories: [Category<Any>]?
}
let myOffer = Offer()
myOffer.name = "offer1"
let myPromotion = Promotion()
myPromotion.name = "promotion1"
let myCategory1 = Category<Offer>()
myCategory1.name = "Offers"
myCategory1.list?.append(myOffer)
let myCategory2 = Category<Promotion>()
myCategory1.name = "Promotions"
myCategory1.list?.append(myPromotion)
let myCatalog = Catalog()
myCatalog.categories?.append(myOffer)
无法将类型
Category<Offer>
的值转换为预期的参数类型Category<Any>
尝试将myOffer或myPromotion附加到categorys数组时,出现上述错误。