Fix: Static vs Class and Stored Property Errors in Swift

Written by

in

Quick answer: In Swift, both static and class define type-level methods and properties, but subclasses can only override class declarations. Use static for stored type properties and non-overridable methods, because Swift classes do not support class var stored properties. If you need an overridable property on a class, declare it as a computed class var instead.

Why this happens

In Swift, both static and class attach a property or method to the type itself rather than to an instance of that type. You call both directly on the type name, such as MyClass.myMethod(), without creating an object first.

The difference comes down to inheritance and dispatch. A static member is implicitly final, meaning subclasses cannot override it. A class member uses dynamic dispatch and allows subclasses to provide their own implementation using the override keyword.

Because of how Swift manages memory for type-level storage, writing class var myVar = "" triggers a compiler error such as Class stored properties not supported in classes; did you mean 'static'?. Stored type properties require a single, fixed storage location in memory, which conflicts with the idea of a subclass overriding that storage. As a result, Swift limits stored type properties to static and requires class properties to be computed.

How to fix it

1. Replace class var with static var for stored properties

If you want a stored property that belongs to the class itself—such as a shared configuration value, a counter, or a constant—change class var to static var or static let.

import Foundation

class NetworkConfig {
    // Compile error: Class stored properties not supported in classes
    // class var baseURL = "https://api.example.com"

    // Fix: use static for stored type properties
    static var baseURL = "https://api.example.com"
    static let timeout: TimeInterval = 30
}

NetworkConfig.baseURL = "https://staging.example.com"

Use static let whenever the value should not change after initialization, and static var when you need mutable shared state.

2. Use a computed class var when subclasses must override a property

If your class hierarchy requires subclasses to return different values for a type property, keep the class var declaration and turn it into a computed property instead of a stored one.

class BaseScreen {
    class var analyticsName: String {
        return "base_screen"
    }
}

class ProfileScreen: BaseScreen {
    override class var analyticsName: String {
        return "profile_screen"
    }
}

print(BaseScreen.analyticsName)    // "base_screen"
print(ProfileScreen.analyticsName) // "profile_screen"

Computed class var properties do not allocate per-class storage, so the Swift compiler allows subclasses to override them freely.

3. Choose static func vs class func (and final class func) for methods

For type-level methods inside a class, decide whether subclasses should be allowed to customize the behavior:

  • Use static func when the implementation is fixed for the entire hierarchy.
  • Use class func when subclasses should be able to override the method.
  • Use override final class func (or override static func) in a subclass when you want to override a class func once and prevent any further subclasses from overriding it again.
class Animal {
    static func kingdom() -> String {
        return "Animalia"
    }

    class func sound() -> String {
        return "..."
    }

    class func category() -> String {
        return "Generic"
    }
}

class Dog: Animal {
    // Compile error: Cannot override static method
    // override static func kingdom() -> String { return "Other" }

    override class func sound() -> String {
        return "Bark"
    }

    // Prevent subclasses of Dog from overriding category()
    override final class func category() -> String {
        return "Canine"
    }
}

class Puppy: Dog {
    // Compile error: Class method overrides a 'final' class method
    // override class func category() -> String { return "Young Canine" }
}

While both override static func and override final class func stop further overrides in subclasses, override final class func makes your intent clearer because the method originally started as a class func.

4. Use static for structs and enums

The class keyword is only valid inside class definitions. Value types (struct and enum) do not support inheritance in Swift, so trying to use class func or class var inside them produces a compiler error. Always use static for type methods and properties in structs and enums.

struct AppConstants {
    static let appName = "MyApp"

    static func formattedTitle(_ text: String) -> String {
        return "\(appName): \(text)"
    }
}

How to prevent it

  • Default to static: Start with static for type properties and methods unless you have a clear reason for a subclass to override them.
  • Prefer static let for constants: Use static let instead of static var for fixed values so they cannot be mutated accidentally across your app.
  • Use computed class var for subclass metadata: When subclasses need distinct identifiers, titles, or configuration strings, use a read-only computed class var.
  • Seal overrides with final class: When a subclass provides the definitive implementation of an inherited class func, mark it override final class func to avoid unexpected overrides deeper in the hierarchy.

FAQ

Can a struct or enum use class func or class var in Swift?

No. Value types such as struct and enum cannot be subclassed, so the class modifier is not allowed on their members. You must use static func, static var, or static let instead.

Is there a performance difference between static and class methods?

Yes. Because static methods cannot be overridden, the Swift compiler can use static dispatch and potentially inline them. class methods use dynamic dispatch unless the class or method is marked final.

How do I stop a subclass from overriding a class func?

If no subclass should ever override the method, declare it with static func in the base class. If one subclass needs to override it and block further overrides below it, mark that subclass implementation with override final class func.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.