I've got a Classification model. And a TodoItem model , which has a attribute type Classification
@Model
final class Classification: Identifiable, Equatable {
var id: UUID
var image: String
var background: Int
var name: String
init(id: UUID = UUID(), image: String, background: Int, name: String) {
self.id = id
self.image = image
self.background = background
self.name = name
}
static let mockData = Classification(image: "questionmark", background: 0xFF0000, name: "tag")
}
@Model
final class TodoItem: Identifiable {
var id: UUID
var title: String
var startTime: Date
var endTime: Date
var isCompleted: Bool
var isLog: Bool
var classification: Classification?
init(id: UUID = UUID(), title: String, startTime: Date, endTime: Date, isCompleted: Bool, isLog: Bool, classification: Classification?) {
self.id = id
self.title = title
self.startTime = startTime
self.endTime = endTime
self.isCompleted = isCompleted
self.isLog = isLog
self.classification = classification
}
convenience init(title: String, startTime: Date, endTime: Date, classification: Classification?) {
self.init(id: UUID(), title: title, startTime: startTime, endTime: endTime, isCompleted: true, isLog: true, classification: classification)
}
static let mockDataTodo = TodoItem( title: "todo", startTime: Date(), endTime: Date(timeIntervalSinceNow: 3000), isCompleted: false, isLog: false, classification: nil)
static let mockDataLog = TodoItem(title: "log", startTime: Date(timeIntervalSinceNow: -3000), endTime: Date(), classification: nil)
}
on my view ,when i create a todoItem , I need to choose classifications which stored in swiftData through a Picker
@Query private var classifications: [Classification]
@State private var todoItem: TodoItem
Picker("choose tag", selection: $selectedClassification) {
Text("empty").tag(nil as Classification?)
ForEach(classifications) { item in
TagView(classification: item)
.tag(Optional(item))
}
}
.onChange(of: selectedClassification) { oldVlaue, newValue in
if let newValue {
todoItem.classification = newValue
} else {
todoItem.classification = nil
}
}
CircleButton(imageString: "checkmark", width: 35, backgroundColor: .green, imageColor: .white, ifGradient: true)
.onTapGesture {
withAnimation(.snappy) {
modelContext.insert(todoItem)
dismiss()
}
}
I got a CircleButton to execute modelContext.insert, but When I just picked classification, this todoItem will be stored
immediately! I have no idea about this.
But if I do not get classifications from swiftdata, but define a mock classifications myself, it will not be automatically inserted.
// @Query private var classifications: [Classification]
var classifications: [Classification] = [
Classification(image: "checkmark", background: 0xff12df, name: "1"),
Classification(image: "checkmark", background: 0xff12df, name: "2"),
Classification(image: "checkmark", background: 0xff12df, name: "3"),
]