62

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:

NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "someSelector:", userInfo: "someData", repeats: true)

But I can't seem to find a way to do it with my custom objects so that I could call it like:

MyCustomObject.someClassLevelFunction("someArg")

Now, I know we can mix Objective-C w/ Swift and it's possible that the NSTimer class method is a remnant from that interoperability.

Question

  1. Do class level functions exist in Swift?

  2. If yes, how do I define a class level function in Swift?

Logan
  • 52,262
  • 20
  • 99
  • 128
  • `class func functionName() -> returnType` you could have checked an existing framework class to find the answer by the way, something like `defaultCenter()` method for `NSNotificationCenter` class. – nsuinteger Jun 04 '14 at 10:32
  • @NSUInteger - I was only finding Objective-C headers. Can you point me to NSNotificationCenter.swift? – Logan Jun 04 '14 at 14:28
  • okay. try this. type NSNotificationCenter.defaultCenter() in your code then use Command+click to navigate to the swift file – nsuinteger Jun 04 '14 at 17:59
  • @nsuinteger -- Thanks, I was option clicking and it was taking me only to ObjC headers. – Logan Jun 04 '14 at 18:15
  • glad you sorted it out. optionally install ios8 library & xcode library so you will have documentation as well (if not done already). xcode > preferences > downloads > ios8 library & xcode6 library. – nsuinteger Jun 05 '14 at 04:12

5 Answers5

111

Yes, you can create class functions like this:

class func someTypeMethod() {
    //body
}

Although in Swift, they are called Type methods.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • 3
    How do I use a class variable inside one of these type methods without getting `.type does not have a member named `? – ohhh Dec 30 '14 at 19:26
38

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.

ray
  • 1,966
  • 4
  • 24
  • 39
mythz
  • 141,670
  • 29
  • 246
  • 390
15

UPDATED: Thanks to @Logan

With Xcode 6 beta 5 you should use static keyword for structs and class keyword for classes:

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

struct Foo2 {
    static func Bar2() -> String {
        return "Bar2"
    }
}
Community
  • 1
  • 1
k06a
  • 17,755
  • 10
  • 70
  • 110
4

you need to define the method in your class

 class MyClass 
 {
       class func myString() -> String
       {
           return "Welcome"
       }
}

Now you can access it by using Class Name eg:

   MyClass.myString()

this will result as "Welcome".

Carmen
  • 6,177
  • 1
  • 35
  • 40
pravin salame
  • 349
  • 3
  • 4
4

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.

lucaslt89
  • 2,431
  • 1
  • 20
  • 30