[英]Returning the Concrete Type Based on Protocol in Swift
I have following code:我有以下代码:
protocol Vehicle {
func start()
}
class Car: Vehicle {
func start() {
print("Start car")
}
}
class MotorCycle: Vehicle {
func start() {
print("Start MotorCycle")
}
}
let vehicles: [Vehicle] = [Car(), MotorCycle()]
func get<T: Vehicle>() -> some Vehicle {
let result = vehicles.first {
$0 === T.self
}
return result!
}
// so I should be able to do this!
let car = get<Car>().start()
Inside the get
function I want to go iterate through vehicles and return the concrete type of the Vehicle, which is either Car
or MotorCycle
.在
get
函数中,我想遍历车辆并返回 Vehicle 的具体类型,即Car
或MotorCycle
。 Finally, I want to call start
on the returned type.最后,我想对返回的类型调用
start
。 How can I achieve it?我怎样才能实现它?
This is how get
should be written: get
应该这样写:
func get<T: Vehicle>(_ type: T.Type) -> T? {
vehicles.first(where: { $0 is T }) as? T
}
T
s in vehicles
, so we should return an optional T
. vehicles
可能没有任何T
,所以我们应该返回一个可选的T
。is
to check the type in the first(where:)
closure.is
在first(where:)
闭包中检查类型。 Then, cast to T
.T
。<Car>
to a function in Swift.<Car>
这样的类型参数传递给 Swift 中的函数。 Type arguments must always be inferred.type
to help Swift infer what T
is.type
来帮助 Swift 推断T
是什么。 Caller:呼叫者:
get(Car.self)?.start()
You can not compare an instance value with a type like this: $0 === T.self
.您不能将实例值与这样的类型进行比较:
$0 === T.self
。 You can use $0 is T
instead.您可以使用
$0 is T
代替。
And, when calling a generic function, you cannot explicitly specialize a generic function.并且,在调用泛型函数时,您不能显式特化泛型函数。 The type has to be inferred, like @Sweeper said.
必须推断类型,就像@Sweeper 所说的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.