当前位置 : 主页 > 手机开发 > 其它 >

arrays – Swift – 如何存储在泛型类型的Array子类中

来源:互联网 收集:自由互联 发布时间:2021-06-11
所以,我有一个泛型(有限制)类和它的子类的许多子类,当它们是子类时具体的泛型类型. 我想将这些子类的实例存储在一个数组中,这样它们就可以迭代并以相同的方式处理所有这些子类
所以,我有一个泛型(有限制)类和它的子类的许多子类,当它们是子类时具体的泛型类型.

我想将这些子类的实例存储在一个数组中,这样它们就可以迭代并以相同的方式处理所有这些子类,但显然,没有办法将子类转换为泛型超类.

以下是一些说明问题的代码(您可以将其复制粘贴到操场中以查看结果):

// Lets create regular classes
class Fruit {
    var text: String { return "I am some Fruit" }
}

class Apple: Fruit {
    override var text: String { return "I am an Apple" }
}
class Orange: Fruit {
    override var text: String { return "I am an Orange" }
}

// This obviously works:
let test1: Fruit = Apple()
let test2: Fruit = Orange()


// Let's create some generic class
class Tree<T: Fruit> {
    let fruit: T
    init(fruit: T) {
        self.fruit = fruit
    }
}

// Subclasses from the generic class (these work)
class AppleTree: Tree<Apple> {
    convenience init() {
        self.init(fruit: Apple())
    }
}

class OrangeTree: Tree<Orange> {
    convenience init() {
        self.init(fruit: Orange())
    }
}

// This works:
let tree: Tree<Fruit> = Tree(fruit: Apple())
tree.fruit.text               // "I am an Apple"

// This works:
let appleTree1: Tree<Apple> = AppleTree()
appleTree1.fruit.text     // "I am an Apple"

// This fails: "Cannot convert value of type 'AppleTree' to specified type 'Tree<Fruit>'
let appleTree2: Tree<Fruit> = AppleTree()

// This works:
let fruitArray: [Fruit] = [Apple(), Orange()]

// THIS IS MY GOAL:
// This fails: "Cannot convert value of type 'AppleTree' to specified type 'Tree<Fruit>'
let treeArray: [Tree<Fruit>] = [AppleTree(), OrangeTree()]


// Let's try with a generic subclass
class FruitTree<T: Fruit>: Tree<T>{}

// This works:
let genericTree: Tree<Fruit> = FruitTree(fruit: Apple())

// Let's try with a generic but more concrete subclass
class GenericOrangeTree<T: Orange>: Tree<T>{
    convenience init() {
        self.init(fruit: Orange() as! T)
    }
}

// This works:
let genericOrangeTree1 = GenericOrangeTree(fruit: Orange())
let genericOrangeTree2 = GenericOrangeTree()

// This fails: Cannot invoke initializer for type 'GenericOrangeTree<Orange>' with an argument list of type '(fruit: Orange)'
let genericTree2: Tree<Fruit> = GenericOrangeTree(fruit: Orange())

// Again, this fails: "Cannot convert value of type 'GenericOrangeTree<Orange>' to specified type 'Tree<Fruit>'
let genericTreeArray: [Tree<Fruit>] = [GenericOrangeTree()]

我试图做的是通过treeArray变量在示例代码中说明的.

我不明白为什么代码失败时会失败.我的直觉说这应该有效,我无法找到解决这个问题的方法.

TL; DR:我有一个带有一些子类的Generic类,我想要一个填充了子类的Generic类的数组,但编译器抱怨.

您正在混淆Generics实现之前和之后的类型层次结构.使用泛型类/ func,您基本上可以设置一个在编译时解析的模板(编译器宏).

如果你说

Generic<SubClass1>
Generic<SuperClass>

这些由编译器解析为类型:

class Generic_SubClass1 {
  let property : SubClass1
}
class Generic_SuperClass {
  let property : SuperClass
}

在解析泛型之后,这两种类型不共享基本类型,因此不能相互转换.它们是完全分开的.

不确定也没试过,但也许这就是你想要的:

class GenericBase {
  let property : SuperClass
}
class Generic<T: SuperClass> : GenericBase {
  final var typedProperty : T {
    get { return property as T }
    set { property = T }
  }
}

然后,您可以使用GenericBase作为公共祖先,并使用动态类型来检查子类.

P.S.:您的代码有点难以理解,可能使用类似’Fruit’,’Apple’和’Orange’的东西 – 比’Superclass’,’Subclass1′,’Subclass2’更容易阅读;-)

网友评论