How do I declare a class level function in Swift?

后端 未结 5 425
借酒劲吻你
借酒劲吻你 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:49

    From the official Swift 2.1 Doc:

    You indicate type methods by writing the static keyword before the method’s func keyword. Classes may also use the class keyword to allow subclasses to override the superclass’s implementation of that method.

    In a struct, you must use static to define a Type method. For classes, you can use either static or class keyword, depending on if you want to allow your method to be overridden by a subclass or not.

提交回复
热议问题