Global constants file in Swift: everything you wanted to know but were afraid to ask

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why do you need a global constants file in Swift?

\n\n

Every iOS developer has encountered a situation at least once where the same values — server URL, API keys, cell identifiers, paddings — are scattered throughout the project. And then the boss says: "Change the base URL." You open the project, and the quest begins: find all occurrences of "https://old.api.com" and replace them with the new one. Sound familiar?

\n\n

That's exactly what a global constants file in Swift is for — a single place where all "magic numbers" and strings are stored. In this article, we'll break down the most common questions from Stack Overflow (the topic has thousands of views!) and provide clear answers with code examples. Let's go!

\n\n

Question 1: How to create a simple constants file in Swift?

\n\n

The most straightforward way is to use a struct with static properties. Why struct and not class? Because structs are value types, they are lighter and don't require an initializer.

\n\n
// Constants.swift\nimport Foundation\n\nstruct AppConstants {\n    static let baseURL = "https://api.myapp.com/v1"\n    static let apiKey = "your-secret-key-here"\n    static let defaultTimeout: TimeInterval = 30.0\n    static let maxRetryCount = 3\n}
\n\n

Usage is simple: AppConstants.baseURL. That's it, no magic strings. Swift constants with let guarantee that the value won't be accidentally changed.

\n\n

Why not use global variables?

\n\n

You could, of course, write:

\n\n
let baseURL = "https://api.myapp.com/v1"
\n\n

But this is a global constants file in Swift in its most primitive form. The problem: no namespace. If you have 50 constants, you'll quickly get confused about which one belongs where. struct solves this problem elegantly.

\n\n

Question 2: Enum or Struct — which to choose for constants?

\n\n

On Stack Overflow, heated debates are raging. Let's break down both approaches.

\n\n

Struct (popular option)

\n\n
struct UIConstants {\n    static let cornerRadius: CGFloat = 12.0\n    static let defaultPadding: CGFloat = 16.0\n    static let animationDuration: Double = 0.3\n}
\n\n

Pros: familiar, you can create instances (though usually not needed), easy to extend via extension.

\n\n

Cons: technically you can write UIConstants(), which doesn't make sense.

\n\n

Enum (stricter option)

\n\n
enum NetworkConstants {\n    static let baseURL = "https://api.myapp.com/v1"\n    static let apiVersion = "2.0"\n    static let defaultHeaders: [String: String] = [\n        "Content-Type": "application/json"\n    ]\n}
\n\n

Pros: you cannot create an instance — the compiler won't allow it. A clean namespace. Ideal for Swift enum constants.

\n\n

Cons: some consider this a "hack" — using an enum not for its intended purpose.

\n\n

My verdict: If you want to be a "proper" developer and protect against accidental instance creation — use enum. If you work in a team where everyone is used to struct — use struct. Both options are valid.

\n\n

Question 3: How to organize constants by category?

\n\n

When a project grows, one file turns into a dump. The solution — nested structs or separate files for each category.

\n\n

Option A: Nested structs (in one file)

\n\n
struct Constants {\n    struct Network {\n        static let baseURL = "https://api.myapp.com"\n        static let timeout: TimeInterval = 30\n    }\n    \n    struct UI {\n        static let cornerRadius: CGFloat = 8\n        static let buttonHeight: CGFloat = 44\n    }\n    \n    struct Storage {\n        static let userDefaultsKey = "com.myapp.user"\n        static let maxCacheSize =                

Blogs

Book Recommendations