add a static property to a class in swift

To add a static property to a class in Swift, you would define the property using the static keyword. Here is an example:

main.swift
class MyClass {
    static var myStaticProperty = "Hello, world!"
    
    // ... other class members ...
}
108 chars
6 lines

In this example, we define a static property named myStaticProperty that belongs to the MyClass class. This property can be accessed directly from the class itself, without the need to create an instance of the class:

main.swift
print(MyClass.myStaticProperty) // outputs "Hello, world!"
59 chars
2 lines

Note that static properties are shared across all instances of the class, and can be modified from any part of the program.

gistlibby LogSnag