I have a class that conforms to a protocol. But it always fail when I try to cast or verify if it conforms to the protocol.
typealias DeviceHandler = (any Device) -> Void
protocol Device {
var name: String { get }
}
class DevicesViewModel<T: Device> {
var devices = [T]()
func findDevices() {
DevicesManager.findDevices() { device in
// casting is failing
// even if I do device is T, it fails
guard let device = device as? T else { return }
self.devices.append(device)
}
}
}
class DevicesManager {
static func findDevices(completion: @escaping DeviceHandler) {
completion(Computer(name: "Macbook"))
}
}
class Computer: NSObject, Device {
var name: String
init(name: String) {
self.name = name
super.init()
}
}