I have the following issue with using generics in my application. As seen below my GeneralUpdate class enforces a type conformance of T to the ReadingClass, but I cannot assign variable of type Reading in the initializer (marked in the GeneralUpdate class as a comment)
class Reading {
var readingDate: Date!
var enteredDate: Date!
init(readingDate: Date, endDate: Date) {
self.readingDate = readingDate
self.enteredDate = endDate
}
}
class GeneralUpdate<T: Reading> {
var readingType: ReadingType!
var dataSource: DataSource!
var readings: [T]
init(readingType: ReadingType, dataSource: DataSource, readings: [Reading]) {
self.readingType = readingType
self.dataSource = dataSource
self.readings = readings //produces error "Cannot assign value of type '[Reading]' to type '[_]'"
}
}
I am not quite sure why this is. I need the reading property to be generic since it can hold different types of readings that are subclassed from the Reading class. I'm new to swift generics and would like some help on how to properly achieve this
initmethod with non-optional values. Delete the exclamation marks. No, you won't get a compiler error.