static let properties and refer to each value through the type name, such as AppConstants.welcomeNotification. For notification names, consider extending Notification.Name so the constant has the type expected by NotificationCenter.Why this happens
An Objective-C project can declare shared string constants in a header and define them in an implementation file. Swift does not require that declaration-and-definition split. A constant declared at file scope can be used elsewhere in the same module, subject to access control.
The practical problem is organization: a collection of unrelated global names can become hard to find and easy to confuse with local variables. A namespace groups shared values under a clear name, while keeping the underlying strings in one place.
How to fix it
1. Group constants in a struct
Create a Swift file such as AppConstants.swift and add a struct with static properties:
struct AppConstants {
static let welcomeNotification = "com.example.welcome"
static let hasCompletedSetupKey = "hasCompletedSetup"
}
Use the constants from another file in the same module:
print(AppConstants.welcomeNotification)
print(AppConstants.hasCompletedSetupKey)
static makes each property available on the type, so there is no need to create an AppConstants instance. let prevents the property from being reassigned after initialization.
To add this to an existing app:
- Create a Swift source file in the app project.
- Add the struct and move your shared values into
static letproperties. - Replace repeated string literals with references such as
AppConstants.hasCompletedSetupKey. - Make sure the source file belongs to each target that needs to compile it.
When replacing an existing key or notification name, keep its string value unchanged unless you intend to change the behavior. In particular, changing a UserDefaults key means a lookup under the new key will not retrieve the value stored under the old one.
2. Nest related groups as the list grows
A single constants type is useful for a small project, but unrelated values can become difficult to scan. Nest types by purpose instead:
struct AppConstants {
struct DefaultsKey {
static let hasCompletedSetup = "hasCompletedSetup"
static let preferredTheme = "preferredTheme"
}
struct API {
static let baseURL = "https://example.com/api"
}
}
Callers now identify both the category and the value:
import Foundation
let defaults = UserDefaults.standard
defaults.set(true, forKey: AppConstants.DefaultsKey.hasCompletedSetup)
let completed = defaults.bool(
forKey: AppConstants.DefaultsKey.hasCompletedSetup
)
print(completed)
The extra grouping helps distinguish a defaults key from an API value without putting every constant into the same flat namespace. You can also use separate top-level types, such as DefaultsKeys and APIConstants, if they fit your project better.
A struct is the direct solution, but an enum with no cases is another option for a namespace that cannot be instantiated:
enum DefaultsKeys {
static let preferredTheme = "preferredTheme"
}
Both approaches let you access a static constant through its type. Pick one style and use it consistently.
3. Use a typed constant for notification names
A string works as a storage value, but NotificationCenter uses Notification.Name for notification names. Extending that type gives callers a typed value directly:
import Foundation
extension Notification.Name {
static let welcomeRequested = Notification.Name(
"com.example.welcomeRequested"
)
}
Post the notification using the shared name:
NotificationCenter.default.post(
name: .welcomeRequested,
object: nil
)
To observe it, use the same constant and retain the returned token for as long as observation is needed:
let token = NotificationCenter.default.addObserver(
forName: .welcomeRequested,
object: nil,
queue: nil
) { _ in
print("Welcome requested")
}
// When observation is no longer needed:
NotificationCenter.default.removeObserver(token)
Using the same typed name at both sites avoids accidentally posting and observing different string literals. If you already have an established notification string, use that exact string in the Notification.Name initializer to preserve compatibility with existing posters and observers.
4. Choose the right visibility
Swift declarations are internal by default. That is usually enough for an app: other files in the same module can use the constants without an access modifier.
Use private for values that should only be implementation details of the enclosing declaration. For example, a public-facing API constant can be assembled from private pieces:
struct APIConstants {
private static let host = "https://example.com"
private static let apiPath = "/api"
static let baseURL = host + apiPath
}
If the constants are defined in a framework or package module and must be accessed from a different module, expose the type and the required members as public:
public enum SharedKeys {
public static let preferredTheme = "preferredTheme"
}
Import that module in the consuming target. Making a type public does not automatically make all its members public, so mark each member that forms part of the intended interface.
A constants file is an organization tool, not a secret store. Values included in an app can be extracted from its distributed files, so do not rely on private or a separate Swift file to protect credentials.
How to prevent it
- Replace repeated shared literals with a single named constant.
- Group constants by purpose instead of building one large, unrelated list.
- Use
Notification.Nameconstants when working withNotificationCenter. - Preserve existing defaults keys and notification strings when refactoring.
- Keep internal implementation values private and expose only the constants another module needs.
FAQ
Do Swift constants have to be inside a struct?
No. You can declare a let at file scope and use it elsewhere in the same module, subject to access control. A struct or enum is useful when you want namespacing and related groups.
Should I use static let or static var?
Use static let when the value should not be reassigned. Use static var only when you need a mutable or computed property; shared mutable state requires additional care when accessed concurrently.
Can I put UserDefaults values in the constants file?
Put the keys there, then read and write the values through UserDefaults. The key identifies the stored setting; a Swift constant does not itself persist the user’s choice.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.