【发布时间】:2018-05-18 23:34:41
【问题描述】:
我有一个带有类 func 的基类:
class BaseModel
{
class func getObjectWithId(_ id: String, completionHandler: @escaping (<???>) -> ())
{
// does query, calls completionHandler with matching object
}
}
我有两个子类:
class Cat: BaseModel { //whatever }
class Dog: BaseModel { //whatever }
我希望能够在子类上调用该类方法,并使用子类类型的对象调用完成处理程序:
Cat.getObjectWithId("1") { (theCat) in
// I want theCat to be of type Cat, not BaseModel
}
Dog.getObjectWithId("1") { (theDog) in
// I want theDog to be of type Dog, not BaseModel
}
我修改了泛型,但承认这不是我的强项...
我不想在每个基类 b/c 中为此编写代码,除了类型之外它是相同的(即我不希望每个孩子都必须实现一个协议)。
我不想要一个将类型作为参数的类方法。我真的很喜欢我的示例方法调用的简单性。
【问题讨论】: