Swift - 类和func关键字

Pat*_*lly 3 swift

以这种方式定义函数究竟意味着什么:

class Foo {
    private class func bar() {
         //do something cool
    }
}
Run Code Online (Sandbox Code Playgroud)

换句话说,第二个class关键字的目的是什么?

使用Swift 2.1.

Lac*_*che 6

classstatic方法是通过类型而不是实例调用的.

let x = NSString.pathWithComponents(["/", "usr", "local", "bin", "brew"])
Run Code Online (Sandbox Code Playgroud)

任何类型都可以有static方法,class方法可能只出现在类上.子类可以覆盖class方法但不能覆盖方法static.

class Foo {
    class func bar() -> String {
        return "foo bar"
    }
    static func baz() -> String {
        return "foo baz"
    }
}
class Bar: Foo {
    override class func bar() -> String {
        return "bar bar"
    }
}
Run Code Online (Sandbox Code Playgroud)