在Swift数组中存储通用对象 [英] Storing generic objects in Swift Array
问题描述
class Model {}
class SubModel:Model {}
class Collection< T:Model> {}
let collection = Collection< SubModel>()
var collections = [Collection]()//也试过[Collection< Model>]()
collections.append (集合)
编译器在append调用失败。消息是:'SubModel'与'Model'不相同。
根据我在泛型中使用的所有其他语言的经验,这应该起作用。作为类型SubMode的集合应该总是可以强制转换为类型Model的集合。
任何人都会遇到这个问题?解决方法?
这是因为Swift不支持协方差,例如:
class Animal {}
class Cat:Animal {}
在您的具体示例中,您可以使用协议和非泛型集合类来解决此限制。 实际上,对于您的情况,您可以将 Wrote the following little test: The compiler fails on the append call. The message is: 'SubModel' is not identical to 'Model'.
Based on my experience in all other languages I have used with generics, this should work. As a Collection of type SubMode should always be coercible to a Collection of type Model.
Anyone run into this? Workarounds? This is happening because Swift does not support covariance in respect of generic type parameters. For example: In your specific example, you can use a protocol and a non-generic Collection class to get around this limitation. Other situations might require more creativity. Actually, in your case, you could just make the element of 这篇关于在Swift数组中存储通用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Cat
显然是 Animal
的子类型。但是,这并不意味着,例如 Array< Cat>
是 Array< Animal>
的子类型。在支持泛型类型参数(如C#)的协方差的语言中, Array< Cat>
将是 Array< Animal> $的子类型c $ c
协议珍藏{}
类型号:珍藏{}
类SubModel:Model {}
class Collection {
func append(element:Collectible){
// blah
}
}
var collection = Collection()
collection.append(Model())
collection.append(SubModel())
var collections = [collection]
Collection
的元素设为一个 Model
,即 func append(element:Model)
,但我这样做是为了强调协议可以有时用于解决协方差的限制。class Model { }
class SubModel : Model {}
class Collection<T: Model> {}
let collection = Collection<SubModel>()
var collections = [Collection]() // Also tried [Collection<Model>]()
collections.append(collection)
class Animal {}
class Cat : Animal {}
Cat
is clearly a subtype of Animal
. However, this does not mean that, for instance, Array<Cat>
is a subtype of Array<Animal>
. In a language that supports covariance for generic type parameters, such as C#, Array<Cat>
would be a subtype of Array<Animal>
.protocol Collectible {}
class Model : Collectible {}
class SubModel : Model {}
class Collection {
func append(element: Collectible) {
// blah
}
}
var collection = Collection()
collection.append(Model())
collection.append(SubModel())
var collections = [collection]
Collection
be a Model
, i.e., func append(element: Model)
, but I did it this way to stress that protocols can sometimes be used to get around covariance limitations.