具有多个不同类型的目录的目录

时间:2019-06-30 14:15:57

标签: arrays swift multiple-instances

尝试创建可存储不同类型类别的目录。

尝试使用泛型和/或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数组时,出现上述错误。

0 个答案:

没有答案