Before we wrap up this chapter, let's look at offering the user a way to choose the location in which a file is to be saved, and to load a file from any location in the file hierarchy.
This goes beyond what is included in the Foundation framework; we need Cocoa, so we need to modify the CustomFileManager
import
statement as follows:
import Cocoa
Creating the Open
dialog box could hardly be easier. Add the following method to the CustomFileManager
class, using a new extension:
extension CustomFileManager { func openFile() -> URL? { let myFileDialog = NSOpenPanel() myFileDialog.runModal() return myFileDialog.url } }
As you can see, this returns the URL of the file chosen by the user.
The dialog box should look familiar:
Note that there is no New Folder
button, because we are searching for a file, not saving one. Also note that the Open
button is grayed out as long as a directory is selected. This is also because we are searching...