0
0
iOS Swiftmobile~10 mins

SwiftData setup (modern persistence) in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the framework needed for SwiftData.

iOS Swift
import [1]
Drag options to blanks, or click blank then click option'
ASwiftData
BFoundation
CUIKit
DSwiftUI
Attempts:
3 left
💡 Hint
Common Mistakes
Importing UIKit or SwiftUI instead of SwiftData.
Forgetting to import any framework.
2fill in blank
medium

Complete the code to declare a SwiftData model class named 'Note'.

iOS Swift
@Model
class [1] {
  var text: String
  init(text: String) {
    self.text = text
  }
}
Drag options to blanks, or click blank then click option'
ANoteModel
Bnote
CNote
DNotes
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'note' for the class name.
Using plural 'Notes' for a single model.
3fill in blank
hard

Fix the error in the code to create a SwiftData container in the app struct.

iOS Swift
var body: some Scene {
  WindowGroup {
    ContentView()
      .modelContainer(for: [1].self)
  }
}
Drag options to blanks, or click blank then click option'
Anote
BNote
CNotes
DNoteModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names that don't match the model class.
Forgetting to add .self after the model name.
4fill in blank
hard

Fill both blanks to declare a SwiftData model with an integer id and a string title.

iOS Swift
@Model
class Task {
  var id: [1]
  var title: [2]
  init(id: Int, title: String) {
    self.id = id
    self.title = title
  }
}
Drag options to blanks, or click blank then click option'
AInt
BString
CDouble
DBool
Attempts:
3 left
💡 Hint
Common Mistakes
Using String for id or Int for title.
Using unsupported types like Double or Bool here.
5fill in blank
hard

Fill all three blanks to create a SwiftData model with a UUID id, a string name, and a boolean done flag.

iOS Swift
@Model
class Item {
  var id: [1] = [2]()
  var name: [3]
  var done: Bool = false
  init(name: String) {
    self.name = name
  }
}
Drag options to blanks, or click blank then click option'
AUUID
CString
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using Int for id instead of UUID.
Not initializing UUID with UUID().
Using Int for name instead of String.