A concise, runnable Swift Playground demonstrating static type properties.
In Swift, a static property belongs to the type itself rather than to any single instance β every instance shares the one stored value. This Playground is a short, runnable learning example that shows exactly that: a struct with a static counter that is incremented from the initializer, so the value keeps climbing across every instance created.
It is meant as a quick reference for understanding the difference between instance properties and type-level (static) properties.
- Declaring a
staticstored property on astruct. - How a
staticproperty is shared across all instances rather than copied per-instance. - Mutating a type property from inside an
init, so it accumulates as new instances are created. - Accessing a
staticproperty through the type name (StaticStruct.property2) versus reading an instance property (staticObject1.property).
git clone https://github.com/ahmetbostanciklioglu/Static.git
cd StaticUnzip StaticTypes.playground.zip, then open StaticTypes.playground in Xcode. The Playground executes automatically and prints the results in the console / sidebar.
- Xcode 12 or later
- Swift 5.0+
- Runs on the iOS Playground target
Ahmet BostancΔ±klΔ±oΔlu β @ahmetbostanciklioglu Β· ahmetbostancikli@gmail.com
β If this helped you, consider giving the repo a star!