NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

What’s the point of static properties and methods in Swift?

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Most people learning Swift immediately see the value of regular properties and methods, but struggle to understand why static properties and methods would be useful. It’s certainly true that they are less useful than regular properties and methods, but they are still fairly common in Swift code.

One common use for static properties and methods is to store common functionality you use across an entire app. For example, I make an app called Unwrap, which is a free iOS app for folks learning Swift. In the app I want to store some common information, such as the URL to the app on the App Store, so I can reference that anywhere the app needs it. So, I have code like this storing my data:

struct Unwrap {
    static let appURL = ""

That way I can write Unwrap.appURL when someone shares something from the app, which helps other folks discover the app. Without the static keyword I’d need to make a new instance of the Unwrap struct just to read the fixed app URL, which isn’t really necessary.

I also use both a static property and a static method to store some random entropy in the same struct, like this:

static var entropy = Int.random(in: 1...1000)

static func getEntropy() -> Int {
    entropy += 1
    return entropy

Random entropy is some randomness collected by software to make random number generation more effective, but I cheat a little in my app because I don’t want truly random data. The app is designed to give you various Swift tests in a random order, but if it were truly random then it’s likely you’d see the same question back to back sometimes. I don’t want that, so my entropy actually makes randomness less random so we get a fairer spread of questions. So, what my code does is store an entropy integer that starts off random, but increments by 1 every time getEntropy() is called.

This “fair random” entropy is used throughout the app so that duplicates won’t appear, so again they are shared statically by the Unwrap struct so everywhere can access them.

Before I move on, there are two more things I want to mention that might interest you.

First, my Unwrap struct isn’t really a struct at all – I actually declare it as an enum rather than a struct. The enum doesn’t have any cases, but it’s a better choice than a struct here because I don’t ever want to create an instance of this type – there’s no reason to. Making an enum stops this from happening, which helps clarify my intent.

Second, because I have a dedicated getEntropy() method, I actually ask Swift to restrict access to the entropy so that I can’t access it from anywhere. This is called access control, and look like this in Swift:

private static var entropy = Int.random(in: 1...1000)

We’ll be looking more at access control in the very next chapter.

Hacking with Swift is sponsored by Essential Developer

SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.3/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.