Swift泛型和子类 [英] Swift generics and subclasses
问题描述
我的问题可能很简单,但它让我感到困惑了一些:
想象一下,我有一个不同的对象数组,它们都有一个共同的父类 MyClass
。
var values = [MyClass]()
然而,它们都有其特定的子类,例如 MyClassSubclass
。
values.append(MyClassSubclass())
现在我想创建一个泛型方法,返回该数组中的第一个对象,该对象的类型为 MyClassSubclass
。我想阻止对象的转换,但是有一个泛型方法,它将子类对象作为T参数,并返回该数组中第一个出现的子类T.
我想过类似的东西(但肯定不行):
func getFirst< T:MyClass>(_ ofType :T.Type) - > T'
我想我只是卡住了,我不知道要搜索什么,所以如果有人可以帮助我,我将不胜感激。
编辑 基于上述值的示例:
class MyClass {}
class MyClassSubclass:MyClass {} $ b $ class MyClassSubclass2:MyClass {}
var values = [MyClass]()
values.append(MyClassSubclass())
values.append(MyClassSubclass2())
//返回出现的第一个类元素这里作为子类型
func getFirst< T>(_ ofType:T.Type) - > T?{}
感谢
来检查元素是否为给定类型:
func getFirst< T:MyClass>(ofType:T.Type) - > T' {
为元素值{
如果让item = elem为? T {
退货项目
}
}
退货零
}
使用来区分和
作为
样式可以简化:
func getFirst< T:MyClass>(ofType:T.Type) - > T' {
用于将项目作为T中的值赋予值{
返回项目
}
返回无
}
另一种方法是使用 flatMap
查找给定
类型的所有项并返回第一个one:
func getFirst< T:MyClass>(ofType:T.Type) - > T' {
返回values.flatMap {$ 0 as? T} .first
}
如果数组可能很大并且想要避免创建一个
中间数组,然后您可以使用 lazy
:
func getFirst< T:MyClass>(ofType:T.Type) - > T' {
返回values.lazy.flatMap {$ 0 as? T} .first
}
作为数组扩展方法,这将是
扩展数组{
func getFirst< T>(ofType:T.Type) - > T' {
返回flatMap {$ 0 as? T} .first
}
}
my question might be simple, but it got me puzzled a bit:
Imagine I have an array of different objects which all have a common parent class "MyClass
".
var values = [MyClass]()
However they all have their specific subclass like for example "MyClassSubclass
".
values.append(MyClassSubclass())
I now want to create a generic method returning me the first object inside this array which is of type MyClassSubclass
. I would like to prevent casting the object, but instead have a generic method which takes the subclass object as T parameter and returns me the first occurrence of subclass T inside this array.
I thought of something like this (but surely that does not work):
func getFirst<T: MyClass>(_ ofType : T.Type) -> T?
I guess I'm just stuck and I don't know what to search for, so if someone could help me I would greatly appreciate it.
Edit Example based on the above values:
class MyClass {}
class MyClassSubclass : MyClass {}
class MyClassSubclass2 : MyClass{}
var values = [MyClass]()
values.append(MyClassSubclass())
values.append(MyClassSubclass2())
//Return the first class element appearing here as a subclass type
func getFirst<T>(_ ofType : T.Type) -> T?{}
Thanks
One approach is to iterate over the array and use optional binding to check if an element is of the given type:
func getFirst<T: MyClass>(ofType: T.Type) -> T? {
for elem in values {
if let item = elem as? T {
return item
}
}
return nil
}
This can be simplified using for case
with the as
pattern:
func getFirst<T: MyClass>(ofType: T.Type) -> T? {
for case let item as T in values {
return item
}
return nil
}
Another approach is to use flatMap
to find all items of the given
type and return the first one:
func getFirst<T: MyClass>(ofType: T.Type) -> T? {
return values.flatMap { $0 as? T }.first
}
If the array can be large and you want to avoid the creation of an
intermediate array then you can use lazy
:
func getFirst<T: MyClass>(ofType: T.Type) -> T? {
return values.lazy.flatMap { $0 as? T }.first
}
As an array extension method this would be
extension Array {
func getFirst<T>(ofType: T.Type) -> T? {
return flatMap { $0 as? T }.first
}
}
这篇关于Swift泛型和子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!