How do I declare a class level function in Swift?

后端 未结 5 429
借酒劲吻你
借酒劲吻你 2020-12-23 09:09

I can\'t seem to find it in the docs, and I\'m wondering if it exists in native Swift. For example, I can call a class level function on an NSTimer like so:

5条回答
  •  感动是毒
    2020-12-23 09:51

    You can define Type methods inside your class with:

    class Foo {
        class func Bar() -> String {
            return "Bar"
        }
    }
    

    Then access them from the class Name, i.e:

    Foo.Bar()
    

    In Swift 2.0 you can use the static keyword which will prevent subclasses from overriding the method. class will allow subclasses to override.

提交回复
热议问题