add a public static property to a class in swift

You can add a public static property to a class in Swift by using the static keyword before the property declaration as shown below:

main.swift
class MyClass {
    static public var myStaticProperty: Int = 0
}
66 chars
4 lines

This creates a static property named myStaticProperty that belongs to the MyClass class. The static keyword ensures that the property can be accessed directly using the class name, without the need to create an instance of the class.

The public access modifier allows the property to be accessed from any source file that imports the module in which the class is defined.

gistlibby LogSnag