相关疑难解决方法(0)

实现泛型的自定义类的数组,不允许具有子类通用的自定义类

我有以下场景

class Human {}
class Child: Human {}
class Person<T: Human> {}

var people = [Person<Human>]()
people.append(Person<Child>())
Run Code Online (Sandbox Code Playgroud)

但是在线上people.append(Person<Child>())我收到了错误

cannot convert value of type 'Person<Child>' to expected argument type 'Person<Human>'
Run Code Online (Sandbox Code Playgroud)

做下面的工作(这似乎是一个相同的情况)真的很奇怪

var myArray = [Array<UIView>]()
myArray.append(Array<UIImageView>())
Run Code Online (Sandbox Code Playgroud)

有人会理解为什么一种方式有效而不是另一种方式?

ios swift

2
推荐指数
1
解决办法
60
查看次数

标签 统计

ios ×1

swift ×1