How to define static constant in a class in swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A static constant in Swift is a value that belongs to the type itself rather than to any individual instance. It is useful for shared configuration, fixed identifiers, and namespaced values that should never change while the app runs.
Define a Static Constant with static let
Inside a class or struct, use static let.
Because the properties are static, you access them through the type name. There is no need to create an APIConfig instance first.
Why Use a Static Constant
A static constant is useful when all instances should see the same immutable value.
That communicates intent clearly: this is metadata about the type or a globally shared constant, not per-object state.
Typical uses include:
- API endpoint roots
- notification names
- default numeric limits
- reusable layout constants
- identifiers for persistence keys
Difference Between let, static let, and class var
These forms solve different problems.
An instance constant belongs to one object:
A static constant belongs to the type:
A class var is a type property that subclasses can override, but it is computed rather than stored in the same way as static let.
If the value is fixed and should not be overridden, static let is usually the best fit.
Static Constants Are Lazily Initialized
Swift initializes static stored properties lazily the first time they are accessed. That means you can safely compute them from other values.
This is convenient for setup code because the property is initialized once and then reused.
Using Static Constants for Namespacing
A static constant also helps group related values without polluting the global namespace.
Even though the article title mentions classes, the same pattern often works well in enums or structs used purely as namespaces.
Access Control Still Applies
You can keep a static constant internal, private, or public like any other property.
That allows you to expose behavior while hiding implementation details.
Prefer Meaningful Names Over Magic Numbers
Static constants are a good way to replace hardcoded values spread throughout a codebase.
Using named constants makes layout code easier to review and change later.
Common Pitfalls
A common mistake is trying to access a static constant through an instance instead of through the type name. Another is using class var when the value is actually fixed and should not be overridden. Developers also sometimes store unrelated app-wide constants in a random class, which makes the codebase harder to navigate. Finally, a static constant can still hold mutable reference types, so immutability of the property does not automatically make the referenced object deeply immutable.
Summary
- Define a static constant with
static let. - Access it through the type name, not an instance.
- Use it for shared immutable values and namespacing.
- Prefer
static letoverclass varwhen overriding is not needed. - Replace scattered magic numbers and string literals with clearly named type-level constants.
Related reading
- How to delete all cookies of UIWebView?
- How to delete all records from table in sqlite with Android?
- How to delete app Build in New iTunes Connect Site?
- How to delete shared preferences data from App in Android
- How to delete URL Types from Xcode?
- How to deselect a selected UITableView cell?
- How to detect first time app launch on an iPhone
- How to detect if app is being built for device or simulator in Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.