Reputation: 3
protocol UtimesListResponseProtocol:HandyJSON{
associatedtype T
var slide: Int {get set}
var top: String {get set}
var bottom: String {get set}
var hasMore: Bool {get set}
var list: [T] {get set}
func add<M:UtimesListResponseProtocol>(data:M)
}
extension UtimesListResponseProtocol{
mutating func add<T:UtimesListResponseProtocol>(data:T){
slide = data.slide
top = data.top
bottom = data.bottom
hasMore = data.hasMore
list.append(data.list)
}
}
I want to add a method of adding arrays in the protocol, but it doesn't seem to work. What should I do, mainly because the data types in array are also generics, what should I do
Upvotes: 0
Views: 52
Reputation: 54735
You have 2 issues: first of all, you need to call append(contentsOf:)
if you want to append an Array
to another. Secondly, you need to make sure the generic type parameters match, since Array
s in Swift can only contain elements of the same type.
extension UtimesListResponseProtocol{
mutating func add<List:UtimesListResponseProtocol>(data:List) where List.T == T {
slide = data.slide
top = data.top
bottom = data.bottom
hasMore = data.hasMore
list.append(contentsOf: data.list)
}
}
Upvotes: 1