call a static property on an instance of a class in swift

To call a static property on an instance of a class in Swift, you need to use the class name instead of the instance name.

Assuming you have a class named MyClass with a static property called myStaticProperty, here's an example of how to call it on an instance of the class:

main.swift
class MyClass {
    static var myStaticProperty = "Hello, world!"
}

let myInstance = MyClass()
let myStaticPropertyValue = MyClass.myStaticProperty // Accessing static property using class name
195 chars
7 lines

In this example, we create an instance of MyClass called myInstance and then access the static property myStaticProperty using the class name MyClass.myStaticProperty. The value of myStaticPropertyValue will be "Hello, world!".

gistlibby LogSnag