0
0
iOS Swiftmobile~20 mins

Why advanced patterns create premium apps in iOS Swift - Build It to Prove It

Choose your learning style9 modes available
Build: Premium Features Demo
This screen shows how using advanced Swift patterns improves app quality and user experience. It demonstrates a clean, reusable UI with MVVM pattern and async data loading.
Target UI
-------------------------
| Premium Features Demo  |
-------------------------
| Loading data...        |
|                       |
| [Feature 1]            |
| [Feature 2]            |
| [Feature 3]            |
|                       |
| [Refresh]              |
-------------------------
Use MVVM pattern to separate UI and data logic
Show a loading indicator while data loads asynchronously
Display a list of premium features after loading
Add a Refresh button to reload data
Use SwiftUI for UI components
Keep code clean and reusable
Starter Code
iOS Swift
import SwiftUI

struct PremiumFeaturesView: View {
    // TODO: Add ViewModel and state properties

    var body: some View {
        VStack {
            Text("Premium Features Demo")
                .font(.title)
                .padding()

            // TODO: Show loading or features list here

            Button("Refresh") {
                // TODO: Reload data
            }
            .padding()
        }
    }
}

struct PremiumFeaturesView_Previews: PreviewProvider {
    static var previews: some View {
        PremiumFeaturesView()
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
iOS Swift
import SwiftUI

class PremiumFeaturesViewModel: ObservableObject {
    @Published var features: [String] = []
    @Published var isLoading = false

    func loadFeatures() async {
        await MainActor.run {
            self.isLoading = true
        }
        try? await Task.sleep(nanoseconds: 2_000_000_000) // simulate 2 seconds delay
        let loadedFeatures = ["Feature 1", "Feature 2", "Feature 3"]
        await MainActor.run {
            self.features = loadedFeatures
            self.isLoading = false
        }
    }
}

struct PremiumFeaturesView: View {
    @StateObject private var viewModel = PremiumFeaturesViewModel()

    var body: some View {
        VStack {
            Text("Premium Features Demo")
                .font(.title)
                .padding()

            if viewModel.isLoading {
                Text("Loading data...")
                    .padding()
            } else {
                List(viewModel.features, id: \.self) { feature in
                    Text(feature)
                }
            }

            Button("Refresh") {
                Task {
                    await viewModel.loadFeatures()
                }
            }
            .padding()
        }
        .task {
            await viewModel.loadFeatures()
        }
    }
}

struct PremiumFeaturesView_Previews: PreviewProvider {
    static var previews: some View {
        PremiumFeaturesView()
    }
}

This solution uses the MVVM pattern by creating a PremiumFeaturesViewModel class that manages the data and loading state. The view observes this model using @StateObject to update the UI reactively.

Data loading is simulated asynchronously with a 2-second delay using Swift concurrency (async/await). While loading, the UI shows a "Loading data..." message. After loading, it displays a list of premium features.

The Refresh button triggers data reload by calling the async load function again.

This separation of concerns and use of modern Swift patterns makes the app code clean, reusable, and responsive, which are key qualities of premium apps.

Final Result
Completed Screen
-------------------------
| Premium Features Demo  |
-------------------------
| Loading data...        |
|                       |
| [Feature 1]            |
| [Feature 2]            |
| [Feature 3]            |
|                       |
| [Refresh]              |
-------------------------
When the screen appears, it shows "Loading data..." for 2 seconds.
After loading, the list of features appears.
Tapping Refresh reloads the data and shows loading again.
Stretch Goal
Add a toggle to switch between light and dark mode for the screen.
💡 Hint
Use SwiftUI's @Environment(\.colorScheme) and a toggle button to switch color schemes.