0
0
iOS Swiftmobile~20 mins

SwiftData setup (modern persistence) in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Notes List
A simple notes app screen that saves and loads notes using SwiftData, Apple's modern persistence framework.
Target UI
-------------------------
| Notes List            |
-------------------------
| + Add Note            |
|-----------------------|
| - Note 1              |
| - Note 2              |
| - Note 3              |
-------------------------
Use SwiftData to save and load notes persistently.
Display a list of notes with their text.
Add a button to add a new note with default text.
Notes should be saved automatically and persist after app restart.
Starter Code
iOS Swift
import SwiftUI
import SwiftData

@Model
class Note {
  var text: String
  init(text: String) {
    self.text = text
  }
}

struct NotesListView: View {
  @Query var notes: [Note]

  var body: some View {
    NavigationView {
      List {
        // TODO: Show notes here
      }
      .navigationTitle("Notes List")
      .toolbar {
        // TODO: Add button to add note
      }
    }
  }
}

@main
struct NotesApp: App {
  var body: some Scene {
    WindowGroup {
      NotesListView()
        .modelContainer(for: [Note.self])
    }
  }
}
Task 1
Task 2
Solution
iOS Swift
import SwiftUI
import SwiftData

@Model
class Note: Identifiable {
  var id = UUID()
  var text: String
  init(text: String) {
    self.text = text
  }
}

struct NotesListView: View {
  @Query var notes: [Note]
  @Environment(\.modelContext) private var modelContext

  var body: some View {
    NavigationView {
      List {
        ForEach(notes) { note in
          Text(note.text)
        }
      }
      .navigationTitle("Notes List")
      .toolbar {
        Button {
          let newNote = Note(text: "New note")
          modelContext.insert(newNote)
        } label: {
          Image(systemName: "plus")
        }
        .accessibilityLabel("Add Note")
      }
    }
  }
}

@main
struct NotesApp: App {
  var body: some Scene {
    WindowGroup {
      NotesListView()
        .modelContainer(for: [Note.self])
    }
  }
}

This app uses SwiftData, Apple's modern persistence framework, to save and load notes automatically.

The @Model attribute defines the Note class as a data model.

The @Query property wrapper fetches all saved notes from the persistent store.

The list displays each note's text.

The '+' button creates a new Note with default text and inserts it into the model context, which saves it automatically.

The .modelContainer(for:) modifier sets up the SwiftData container for persistence.

This setup means notes persist between app launches without extra code.

Final Result
Completed Screen
-------------------------
| Notes List            |
-------------------------
| + Add Note            |
|-----------------------|
| - New note            |
| - Note 1              |
| - Note 2              |
-------------------------
Tapping the '+' button adds a new note with text 'New note' to the list.
Notes remain saved and visible after restarting the app.
Stretch Goal
Add the ability to delete notes by swiping left on a note in the list.
💡 Hint
Use the .onDelete modifier on the List and remove the note from the modelContext.