Before we get coding, we'll assemble a window with a few buttons and a text field, which will facilitate trying out the code as we progress:
- Create a new project using Xcode's
Cocoa Application
template.
- Add six buttons and a text field to the
View Controller Scene
, arranged something like this:
As we move on, we'll make the buttons' titles a little more descriptive.
We won't put any file reading or writing capabilities in the ViewController
class, since that's not its job, so we must create a class to manage file IO:
- Create a new Swift file, and name it
CustomFileManager.swift
. - Add the following class definition to the new file:
class CustomFileManager { static let sharedManager = CustomFileManager() let userDefaults = UserDefaults.standard }
All we're doing here is setting up the class as a singleton (we will only ever need one instance, from wherever we access it in the app), and then declaring a property that will maintain a reference...