create a journaling app where you can write notes and save them once you close the app, allow to edit the note and cancel it in swift

To build a journaling app in Swift with the ability to write notes, save them even when the app is closed, edit the notes, and cancel them, we need to use CoreData to save the notes.

Firstly, create a new Xcode project and select "Single View App". Then, add a new CoreData model file using the "File" menu. In the new model file, create an entity named "Note" with the following attributes:

  • noteID (type: String)
  • content (type: String)
  • timestamp (type: Date)

After creating the entity, generate a subclass of NSManagedObject for it using the "Editor" menu.

Next, create a ViewController with a TextView and two buttons: "Save" and "Cancel". Connect these buttons to IBOutlets and IBActions in the ViewController file.

main.swift
import UIKit
import CoreData

class ViewController: UIViewController {

    @IBOutlet weak var noteTextView: UITextView!
    
    var note: Note?
    
    lazy var persistentContainer: NSPersistentContainer = {
        let container = NSPersistentContainer(name: "Notes")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                print("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()
    
    @IBAction func saveNote() {
        if let content = noteTextView.text, !content.isEmpty {
            if let note = note {
                note.content = content
                note.timestamp = Date()
            } else {
                let context = persistentContainer.viewContext
                let note = Note(context: context)
                note.noteID = UUID().uuidString
                note.content = content
                note.timestamp = Date()
            }
            saveContext()
            navigationController?.popViewController(animated: true)
        }
    }
    
    @IBAction func cancelNote() {
        navigationController?.popViewController(animated: true)
    }
    
    func saveContext() {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                let nserror = error as NSError
                print("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let note = note {
            noteTextView.text = note.content
        }
    }

}
1742 chars
61 lines

In the viewController file, we have a lazy var persistentContainer property to set up the CoreData stack. The saveNote() function checks if the note has any text and then saves the note, editing it if necessary. The cancelNote() function simply returns to the previous ViewController.

Finally, set up a segue from a TableViewCell to the ViewController, passing the note as a parameter:

main.swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "editNoteSegue" {
            if let destinationVC = segue.destination as? ViewController,
                let indexPath = tableView.indexPathForSelectedRow {
                destinationVC.note = notes[indexPath.row]
            }
        }
    }
346 chars
9 lines

Your journaling app is now ready to be run on an iOS device or simulator.

gistlibby LogSnag