A simple TODO app using SwiftData and SwiftUI
SwiftData-Todo
A simple TODO app using SwiftData and SwiftUI
This uses the latest framework SwiftData available in iOS 17. It also makes use of other new SwiftUI APIs such as ContentUnavailableView
for empty states.
The Task object is created as:
@Model
final class Task {
@Attribute(.unique)
var id: UUID
var name: String
var createdAt: Date
var completed: Bool
var color: String
init(name: String, createdAt: Date = Date(), completed: Bool = false, color: TaskColor) {
self.id = UUID()
self.name = name
self.createdAt = createdAt
self.completed = completed
self.color = color.rawValue
}
}
Then simply fetched in the list view as:
@Query(sort: \.createdAt, animation: .smooth)
private var tasks: [Task]
A new Task can be created and persisted like:
let newTask = Task(
name: taskName,
color: taskColor
)
context.insert(newTask)
do {
try context.save()
} catch {
print(error.localizedDescription)
}