call a public static method on an instance of a class in swift

To call a public static method on an instance of a class in Swift, you need to use the class name instead of the instance name, like so:

main.swift
class MyClass {
    public static func myStaticMethod() {
        // Do something
    }
}

let myInstance = MyClass()
MyClass.myStaticMethod() // Call the static method on the class name
187 chars
9 lines

In the example above, we define a class called MyClass with a public static method called myStaticMethod. Then we create an instance of MyClass named myInstance. To call the static method on the instance, we use the class name instead of the instance name (MyClass.myStaticMethod()), since static methods belong to the class, not the instance.

gistlibby LogSnag