101

Class (or static) methods in Objective-C were accomplished using + in declarations.

@interface MyClass : NSObject

+ (void)aClassMethod;
- (void)anInstanceMethod;

@end

How can this be achieved in Swift?

Erik Kerber
  • 5,646
  • 7
  • 38
  • 56

5 Answers5

161

They are called type properties and type methods and you use the class or static keywords.

class Foo {
    var name: String?           // instance property
    static var all = [Foo]()    // static type property
    class var comp: Int {       // computed type property
        return 42
    }

    class func alert() {        // type method
        print("There are \(all.count) foos")
    }
}

Foo.alert()       // There are 0 foos
let f = Foo()
Foo.all.append(f)
Foo.alert()       // There are 1 foos
Pascal
  • 16,846
  • 4
  • 60
  • 69
21

They are called type properties and type methods in Swift and you use the class keyword.
Declaring a class method or Type method in swift :

class SomeClass 
{
     class func someTypeMethod() 
     {
          // type method implementation goes here
     }
}

Accessing that method :

SomeClass.someTypeMethod()

or you can refer Methods in swift

Rohit
  • 1,189
  • 12
  • 23
16

Prepend the declaration with class if it's a class, or with static if it's a structure.

class MyClass : {

    class func aClassMethod() { ... }
    func anInstanceMethod()  { ... }
}
Analog File
  • 5,280
  • 20
  • 23
  • Don't you need the `func` keyword here? – Jamie Forrest Jun 06 '14 at 18:01
  • 1
    of course. Serves me for answering questions while standing on a crowded bus, lol. Corrected. – Analog File Jun 06 '14 at 18:04
  • concise answer. One of the overlooked aspects of a generation of Objective-C code that might not have been ported is that the next person may have to port it over to some version of Swift. This is helpful as I'd completely forgotten about the notion of class methods from Obj-C, and I am in the midst of porting some mess. – truedat101 Mar 11 '21 at 07:38
4

Swift 1.1 doesn't have stored class properties. You can implement it using a closure class property that fetches an associated object tied to the class object. (Only works in classes derived from NSObject.)

private var fooPropertyKey: Int = 0  // value is unimportant; we use var's address

class YourClass: SomeSubclassOfNSObject {

    class var foo: FooType? {  // Swift 1.1 doesn't have stored class properties; change when supported
        get {
            return objc_getAssociatedObject(self, &fooPropertyKey) as FooType?
        }
        set {
            objc_setAssociatedObject(self, &fooPropertyKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
        }
    }

    ....
}
John Wallace
  • 161
  • 1
  • 2
  • I have been learning Swift and wondered if you could attach associated objects to Swift class instances. It sounds like the answer is "sort of". (Yes, but only objects that are a subclass of NSObject.) Thanks for resolving that for me. (Voted) – Duncan C Mar 28 '15 at 17:14
4

Prepend the declaration with class or static if it's a function, or with static if it's a property.

class MyClass {

    class func aClassMethod() { ... }
    static func anInstanceMethod()  { ... }
    static var myArray : [String] = []
}
techloverr
  • 2,597
  • 1
  • 16
  • 28