Getting Started with SwiftData in SwiftUI 5 Tutorial (Tutorial with Source Code)

What are we building?

Remembering dates is hard for me. Yes, you can set a reminder for that date, but that reminder cannot create a sense of urgency. Let's create an application where we can add future events. It would be nice if we could see all the upcoming activities on the main app. Maybe add them on widgets to create a sense of urgency.

For any event, what things do we need to track?

  • Event name
  • date

We'll add more properties in the next article, in which we'll talk about migrations. Now, that's all we need. let's start

API

If you're only interested in SwiftData, feel free to skip this section.

Let's create a very simple interface to display a list of events. Add a plus button, this will open a sheet where we can add new events. After saving we update our main list.

main list

Our list will display a list of events. Each event has a name, a date. So let's create a model to represent our event object.

struct Event {
    let name: String
    let date: Date
}

We can create a very basic list with the given code:

struct ContentView: View {
    var events: [Event] = [.init(name: "Happy Birthday!!", date: .init(timeIntervalSinceNow: 60 * 24 * 24))]
    
    var body: some View {
        VStack {
            ForEach(events, id: \.self) {
                Text

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/131370053