Bird
Raised Fist0
iOS Swiftmobile~20 mins

Build configurations (Debug, Release) in iOS Swift - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Build Configurations Demo
This screen shows a label that changes text depending on the build configuration: Debug or Release. It helps understand how to use different settings for Debug and Release builds in an iOS app.
Target UI
-------------------------
| Build Configurations   |
|-----------------------|
| Current Mode: DEBUG    |
|                       |
| [Button: Show Mode]   |
-------------------------
Display a label that shows 'Current Mode: DEBUG' or 'Current Mode: RELEASE' depending on the build configuration.
Add a button labeled 'Show Mode' that when tapped, updates the label with the current build mode.
Use Swift conditional compilation flags (#if DEBUG) to determine the build mode.
The UI should be simple with a label and a button centered vertically and horizontally.
Starter Code
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var modeText = "Current Mode: UNKNOWN"

    var body: some View {
        VStack(spacing: 20) {
            Text(modeText)
                .font(.title)
                .padding()

            Button("Show Mode") {
                // TODO: Update modeText based on build configuration
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(UIColor.systemBackground))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
Task 1
Solution
iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var modeText = "Current Mode: UNKNOWN"

    var body: some View {
        VStack(spacing: 20) {
            Text(modeText)
                .font(.title)
                .padding()

            Button("Show Mode") {
                #if DEBUG
                modeText = "Current Mode: DEBUG"
                #else
                modeText = "Current Mode: RELEASE"
                #endif
            }
            .padding()
            .background(Color.blue)
            .foregroundColor(.white)
            .cornerRadius(8)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(UIColor.systemBackground))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This app uses SwiftUI to create a simple screen with a label and a button.

The label shows the current build mode, which is updated when the user taps the button.

Inside the button's action, we use Swift's conditional compilation flags #if DEBUG and #else to check if the app is running in Debug or Release mode.

Depending on the build configuration, the label text updates accordingly.

This helps beginners see how to use build configurations to change app behavior.

Final Result
Completed Screen
-------------------------
| Build Configurations   |
|-----------------------|
| Current Mode: DEBUG    |
|                       |
| [Button: Show Mode]   |
-------------------------
When the user taps the 'Show Mode' button, the label updates to show 'Current Mode: DEBUG' if running a Debug build.
If running a Release build, the label updates to 'Current Mode: RELEASE'.
Stretch Goal
Add a toggle to automatically show the build mode label on app launch without pressing the button.
💡 Hint
Use the .onAppear modifier to set the modeText when the view appears.

Practice

(1/5)
1. What is the main purpose of having separate Debug and Release build configurations in an iOS app?
easy
A. To change the app's icon automatically
B. To switch programming languages
C. To create different versions of the app for testing and for users
D. To disable all app features

Solution

  1. Step 1: Understand build configurations

    Debug and Release configurations allow different settings for testing and final app versions.
  2. Step 2: Identify the purpose

    Debug is for testing with extra info; Release is optimized for users without debug info.
  3. Final Answer:

    To create different versions of the app for testing and for users -> Option C
  4. Quick Check:

    Build configurations = different app versions [OK]
Hint: Debug = testing, Release = user version [OK]
Common Mistakes:
  • Thinking build configs change app icons automatically
  • Confusing build configs with programming languages
  • Believing build configs disable features
2. Which Swift code snippet correctly runs only in Debug build configuration?
easy
A. #if DEBUG print("Debug mode") #endif
B. if DEBUG { print("Debug mode") }
C. if #DEBUG { print("Debug mode") }
D. debug { print("Debug mode") }

Solution

  1. Step 1: Recall Swift conditional compilation syntax

    Swift uses #if ... #endif for build config checks.
  2. Step 2: Identify correct syntax

    Only #if DEBUG print("Debug mode") #endif uses #if DEBUG and #endif correctly.
  3. Final Answer:

    #if DEBUG print("Debug mode") #endif -> Option A
  4. Quick Check:

    #if DEBUG ... #endif = correct syntax [OK]
Hint: Use #if DEBUG ... #endif for build checks [OK]
Common Mistakes:
  • Using if DEBUG without #if and #endif
  • Writing #DEBUG instead of #if DEBUG
  • Using unknown keywords like debug
3. Given this Swift code snippet, what will print when running a Release build?
#if DEBUG
print("Debug build")
#else
print("Release build")
#endif
medium
A. Compile error
B. Debug build
C. Nothing prints
D. Release build

Solution

  1. Step 1: Understand conditional compilation

    #if DEBUG runs only in Debug builds; #else runs otherwise.
  2. Step 2: Identify build type

    In Release build, DEBUG is not defined, so #else block runs.
  3. Final Answer:

    Release build -> Option D
  4. Quick Check:

    Release build prints "Release build" [OK]
Hint: Release build runs #else block in #if DEBUG [OK]
Common Mistakes:
  • Assuming Debug print runs in Release
  • Thinking nothing prints in Release
  • Expecting compile error from #if
4. You wrote this code but it causes a compile error:
if #DEBUG {
  print("Debug mode")
}
What is the error and how to fix it?
medium
A. Use #if DEBUG ... #endif instead of if #DEBUG
B. Replace #DEBUG with DEBUG without #
C. Add parentheses after #DEBUG
D. Change if to switch

Solution

  1. Step 1: Identify incorrect syntax

    Swift uses #if ... #endif for build config checks, not if #DEBUG.
  2. Step 2: Correct syntax usage

    Replace if #DEBUG with #if DEBUG ... #endif block.
  3. Final Answer:

    Use #if DEBUG ... #endif instead of if #DEBUG -> Option A
  4. Quick Check:

    Use #if DEBUG ... #endif for build checks [OK]
Hint: Use #if ... #endif, not if #DEBUG [OK]
Common Mistakes:
  • Using if #DEBUG instead of #if DEBUG
  • Adding parentheses after #DEBUG
  • Trying to use switch for build configs
5. You want to log detailed info only in Debug builds but avoid any logging in Release builds. Which approach is best in Swift?
hard
A. Use Release build for testing logs
B. Wrap logging code inside #if DEBUG ... #endif blocks
C. Always log and filter logs manually later
D. Use runtime checks like if isDebugMode { ... } without build flags

Solution

  1. Step 1: Understand build-time code inclusion

    #if DEBUG ... #endif includes code only in Debug builds, excluding it from Release.
  2. Step 2: Compare alternatives

    Runtime checks still include code in Release, wasting resources; logging always wastes resources; Release build is for users, not testing.
  3. Final Answer:

    Wrap logging code inside #if DEBUG ... #endif blocks -> Option B
  4. Quick Check:

    #if DEBUG excludes code from Release [OK]
Hint: Use #if DEBUG to exclude debug code from Release [OK]
Common Mistakes:
  • Using runtime flags that don't remove code in Release
  • Logging always and filtering later wastes resources
  • Using Release build for testing logs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard