如何在类方法中返回self?

Saa*_*man 3 class ios swift

试过这个,它给了我一个错误:

class BaseClass {
    class var testProperty: String {
        return "Original"
    }
    class func testingFunc()->Self {
       return self // error - Can not convert return expression of type "Self.Type" to return the type "Self"
    }
}
Run Code Online (Sandbox Code Playgroud)

任何想法?谢谢

Gon*_*lde 8

在类/静态函数中,self指的是类类型.没有实例可以参考,所以你得到的是类型,它是当前的范围.它在自我引用的实例方法中是不一样的<instance>.self

class Foo {
    class func classMethod() -> Foo.Type {
        return self // means Foo.self
    }

    func instanceMethod() -> Foo {
        return self // means <instance of Foo>.self
    }
}
Run Code Online (Sandbox Code Playgroud)