Container is a generic class that accepts UIView type. VideoContainer and TitleContainer inherit Container respectively. I want to add titleContainer to the subContainer array of VideoContainer, but an error is reported.
<code>public class Container<U: UIView>: NSObject {
public var subContainer: [Container] = []
}
public class VideoContainer: Container<UIView1> {
var titleContainer = TitleContainer()
override init() {
super.init()
self.subContainer.append(titleContainer) // error code
}
}
public class TitleContainer: Container<UIView2> {
}
public class UIView1: UIView {
}
public class UIView2: UIView {
}
</code>
<code>public class Container<U: UIView>: NSObject {
public var subContainer: [Container] = []
}
public class VideoContainer: Container<UIView1> {
var titleContainer = TitleContainer()
override init() {
super.init()
self.subContainer.append(titleContainer) // error code
}
}
public class TitleContainer: Container<UIView2> {
}
public class UIView1: UIView {
}
public class UIView2: UIView {
}
</code>
public class Container<U: UIView>: NSObject {
public var subContainer: [Container] = []
}
public class VideoContainer: Container<UIView1> {
var titleContainer = TitleContainer()
override init() {
super.init()
self.subContainer.append(titleContainer) // error code
}
}
public class TitleContainer: Container<UIView2> {
}
public class UIView1: UIView {
}
public class UIView2: UIView {
}
How to add another generic type to an array of generic types normally?
New contributor
Uzy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1