Building a Notes App with FileManager in Swift
A notes app is a useful first project for learning how an iOS application stores, reads, updates and deletes information. Instead of sending data to a server, you can keep each note in the app’s sandbox with Swift and FileManager. This creates a small but realistic project that teaches persistence, UIKit, Codable and file-system safety.
For an Australian audience, offline storage is especially practical. A student travelling between Sydney and Melbourne may want notes on a train without reliable connectivity, while someone in regional Queensland, Western Australia or the Northern Territory cannot always depend on fast mobile data. A local notes app also avoids building a backend before the core product is ready.
Choosing A Simple Data Model
Begin with a model that represents the information your application actually needs. A note could contain an identifier, a title, the main text and dates showing when it was created or edited. Swift’s Codable protocol can convert this structure into JSON, which is easy to save as a file and inspect during development.
struct Note: Codable, Identifiable {
let id: UUID
var title: String
var body: String
let createdAt: Date
var updatedAt: Date
}
The UUID gives every note a stable identity, even when two notes have the same title. Keeping createdAt and updatedAt as Date values is better than storing formatted strings because the interface can later display dates according to the user’s locale. For example, an Australian user might see 12/03/2026, while the stored value remains consistent.
A small project like this is a natural next step after building a first UIKit app, because it adds persistence without requiring a complicated architecture.
Finding A Safe Location In The Sandbox
iOS applications receive a private sandbox, and FileManager provides the tools needed to work with directories inside it. The documents directory is a sensible location for user-created notes because its contents are considered application data rather than temporary cache files.
final class NotesStore {
private let fileManager = FileManager.default
private var notesURL: URL {
let directory = fileManager.urls(
for: .documentDirectory,
in: .userDomainMask
)[0]
return directory.appendingPathComponent("notes.json")
}
}
The URL should be generated by the system instead of being hard-coded. A hard-coded path can fail across devices, simulators and iOS versions. The app’s container may have a different location on an iPhone in Brisbane than it does in an Xcode simulator running on a Mac in Adelaide.
You can use the library directory for supporting files, but the documents directory communicates intent clearly. Avoid saving personal notes in the temporary directory because iOS may remove temporary files when storage is under pressure.
Encoding And Saving Notes
Saving begins by converting an array of Note values into JSON data. JSONEncoder handles the conversion, while FileManager writes the resulting data to disk. A throwing function is useful because disk operations can fail and the caller should decide how to respond.
func save(_ notes: [Note]) throws {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
let data = try encoder.encode(notes)
try data.write(to: notesURL, options: [.atomic])
}
The .atomic option writes the data safely by creating a temporary file and replacing the previous version after the write succeeds. If the application closes during the operation, this reduces the risk of leaving behind a partly written JSON document.
For a small notes app, rewriting one file after each edit is perfectly reasonable. A user might create a few hundred short notes, which is a tiny amount of data for a modern iPhone. Larger apps would eventually need a database such as Core Data or SwiftData, but starting with a simple file keeps the storage layer easy to understand.
Loading Data When The App Starts
Loading should handle two normal situations: the file exists and contains valid notes, or the file does not exist because the user is opening the app for the first time. Treating a missing file as an empty collection prevents a new installation from appearing broken.
func load() throws -> [Note] {
guard fileManager.fileExists(atPath: notesURL.path) else {
return []
}
let data = try Data(contentsOf: notesURL)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return try decoder.decode([Note].self, from: data)
}
Call this method when the view controller or model layer is created, then place the result in an in-memory array. The table view should display that array rather than reading the file repeatedly. This separation makes the interface responsive and gives the storage code one clear responsibility.
A corrupted file is uncommon during normal use, but it should still be handled. You can show an error message, preserve the damaged file for debugging and offer an empty state instead of silently deleting the user’s content.
Connecting FileManager To UIKit
A typical UIKit design uses a UITableView or UICollectionView to list notes. The view controller asks NotesStore for saved values, creates cells from the current array and opens an editor when a user taps a row. The storage object should not know anything about labels, navigation controllers or table-view cells.
When a user creates a note, build a new Note, append it to the array and call save. When an existing note changes, find it by its id, replace the value and save again. For deletion, remove the matching element and persist the updated collection. This simple flow supports the main CRUD operations: create, read, update and delete.
Use tableView(_:commit:forRowAt:) for swipe-to-delete, then call tableView.deleteRows after the array and file have been updated. Keeping the data mutation and visual update in the same action helps prevent rows from becoming out of sync with the stored notes.
Handling Errors And App Lifecycle
File operations can throw errors because of a full disk, invalid data or an unexpected sandbox problem. Presenting a clear alert is better than ignoring the error. A message such as “Your note could not be saved” gives the user useful information without exposing technical details.
Saving after every keystroke is unnecessary and can create excessive file activity. A better approach is to save when the user taps Done, leaves the editor or finishes an editing session. You can also save when the application moves into the background, although this should support regular saves rather than replace them.
Test the app by creating notes, force-quitting it, reopening it and deleting records. Run the same checks on a simulator and a physical iPhone. Test long text, an empty title, unusual punctuation and multiple rapid edits. For junior developers building a portfolio project, these tests demonstrate more care than a screen that merely looks complete.
Improving The Experience For Real Users
A basic JSON file is enough for a local prototype, but the interface still needs thoughtful details. Show an empty-state label such as “No notes yet”, provide a clear New Note button and sort records by updatedAt so recently edited content appears first. Use Dynamic Type and accessible labels so the app works well with larger text and VoiceOver.
Avoid assuming that every user has a constant connection or wants an account. Local-first behaviour suits quick notes taken on a flight from Perth to Sydney, during a school day in Canberra or while travelling through an area with limited coverage. If cloud sync is added later, the local file can remain the immediate source of truth while synchronisation runs separately.
Privacy also matters in the Australian market. Notes may contain school work, addresses or personal plans, so avoid logging their contents in production. Consider file protection with FileProtectionType.complete when appropriate, and explain clearly if a future version sends data to a remote service. Local storage is a useful privacy feature, but it is not a substitute for careful device security.
Knowing When To Move Beyond A File
FileManager works well when the data set is small and the model is straightforward. It gives you a practical understanding of directories, URLs, encoding and persistence without hiding the process behind a framework. That makes it a strong learning project for someone moving from basic UIKit screens towards complete applications.
The approach becomes less suitable when notes need search across large collections, relationships, conflict resolution or simultaneous writes. A database can index content and update individual records instead of rewriting one large JSON file. SwiftData offers a modern Apple-focused option, while Core Data remains widely used in established iOS projects.
Keep the storage API behind methods such as load, save, add, update and delete. If the app later moves to SwiftData, the view controllers will need fewer changes because they depend on the store’s interface rather than its internal implementation.
A dependable first version needs only a model, a sandbox URL, JSON encoding, safe writes and a UIKit screen that reflects the current array. Build those pieces in order, test the file after relaunching, and use FileManager for small offline collections before reaching for a database.