create a note taking app using swiftui and userdefaults in swift

To create a note-taking app using SwiftUI and UserDefaults in Swift, follow these steps:

  1. Create a new SwiftUI project in Xcode.
  2. Create a new Swift file and name it "Note.swift". This will be the model for our notes.
  3. In the "Note.swift" file, create a struct with two properties: "title" (String) and "content" (String).
  4. In the project navigator, select the "Info.plist" file and add a new row with the key "NSUserActivityTypes" and the value "com.yourdomain.yourapp.notes".
  5. Open the "ContentView.swift" file and create a @State property called "notes" which is an Array of "Note" structs.
  6. In the "ContentView.swift" file, create a NavigationView with a List that displays the titles of the notes.
  7. In the "ContentView.swift" file, create a NavigationLink for each note in the List.
  8. When the user taps on a note, the app should navigate to a DetailView that displays the note's title and content.
  9. In the "DetailView.swift" file, create a @State property called "editable" that is set to false by default.
  10. Add a Button to the DetailView that allows the user to toggle the "editable" property.
  11. When the user taps the "Done" button, the DetailView should update the note's title and content and navigate back to the ContentView.

Here is the code for the various files:

Note.swift:

main.swift
struct Note: Codable {
    var title: String
    var content: String
}
71 chars
5 lines

ContentView.swift:

main.swift
import SwiftUI

struct ContentView: View {
    
    @State private var notes: [Note] = UserDefaults.standard.object(forKey: "notes") as? [Note] ?? []
    
    var body: some View {
        NavigationView {
            List(notes, id: \.title) { note in
                NavigationLink(destination: DetailView(note: note)) {
                    Text(note.title)
                }
            }
            .navigationBarTitle("Notes")
            .navigationBarItems(trailing: Button(action: {
                let newNote = Note(title: "New note", content: "")
                notes.append(newNote)
                UserDefaults.standard.set(try? PropertyListEncoder().encode(notes), forKey:"notes")
            }) {
                Image(systemName: "plus")
            })
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
904 chars
31 lines

DetailView.swift:

main.swift
import SwiftUI

struct DetailView: View {
    
    @State private var editable = false
    var note: Note
    
    var body: some View {
        VStack {
            TextField("Title", text: $note.title)
                .font(.title)
                .disabled(!editable)
            TextEditor(text: $note.content)
                .disabled(!editable)
            Spacer()
            HStack {
                Spacer()
                Button(action: {
                    editable.toggle()
                }) {
                    Text(editable ? "Done" : "Edit")
                }
            }
        }
        .padding()
        .navigationBarTitle(Text(note.title), displayMode: .inline)
    }
}

struct DetailView_Previews: PreviewProvider {
    static var previews: some View {
        DetailView(note: Note(title: "Sample note", content: "This is a sample note."))
    }
}
881 chars
35 lines

This should give you a basic idea on how to create a note-taking app using SwiftUI and UserDefaults in Swift.

gistlibby LogSnag