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.swift1742 chars61 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.swift346 chars9 lines
Your journaling app is now ready to be run on an iOS device or simulator.
gistlibby LogSnag