How do I make class methods / properties in Swift?

Swift

Swift Problem Overview


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?

Swift Solutions


Solution 1 - Swift

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

Solution 2 - Swift

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

Solution 3 - Swift

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()  { ... }
}

Solution 4 - Swift

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))
        }
    }

    ....
}

Solution 5 - Swift

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] = []
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErik KerberView Question on Stackoverflow
Solution 1 - SwiftPascalView Answer on Stackoverflow
Solution 2 - SwiftRohitView Answer on Stackoverflow
Solution 3 - SwiftAnalog FileView Answer on Stackoverflow
Solution 4 - SwiftJohn WallaceView Answer on Stackoverflow
Solution 5 - SwifttechloverrView Answer on Stackoverflow