Here's an example of a SwiftUI view that displays a list of tasks using the Task model:
swift
import SwiftUI
struct Task: Codable {
let id: Int
let description: String
let completedAt: Date?
}
struct TaskList: View {
@State var tasks: [Task] = []
var body: some View {
NavigationView {
List(tasks) { task in
VStack(alignment: .leading) {
Text(task.description)
.font(.headline)
.lineLimit(2)
.truncationMode(.tail)
Text(task.id.description)
.font(.subheadline)
}
.padding()
}
.navigationBarTitle("Tasks")
}
}
}
This view uses the NavigationView to present the list of tasks, and the List view to display each task. The VStack view is used to place the description of each task on the left and the ID of each task on the right.
The Text view is used to display the description and ID of each task. The Text(task.description) view is used to display the description, and the Text(task.id.description) view is used to display the ID.
You can also add a button to edit or delete the task.
swift
import SwiftUI
struct Task: Codable {
let id: Int
let description: String
let completedAt: Date?
}
struct TaskList: View {
@State var tasks: [Task] = []
var body: some View {
NavigationView {
List(tasks) { task in
VStack(alignment: .leading) {
Text(task.description)
.font(.headline)
.lineLimit(2)
.truncationMode(.tail)
Text(task.id.description)
.font(.subheadline)
}
.padding()
}
.navigationBarTitle