Tutorial 3: MyLocations V5.0 page 151 - applicationDocumentsDirectory

There is a little mistake in getting applicationDocumentsDirectory constant in Functions.swift. In the book there is a version for the Swift 2. Correct version for the Swift 3 will be like this:

let applicationDocumentsDirectory: URL = {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    return paths[0]
}()

Thanks! Will fix. :smiley:

Response might have been redundant

Just ran into this. How did you determine the correct method syntax?
There was no Code Completion for this one to clarify.

Cmd+Clicking on “FileManager” led me to see the definitions with spaces in parameter names:

@available(iOS 4.0, *) open func urls(for directory: FileManager.SearchPathDirectory, in domainMask: FileManager.SearchPathDomainMask) -> [URL]

I tried to amend the tutorial code based on that to:
FileManager.defaults.urls(for directory: .documentDirectory in domainMask: .userDomainMask)

Apple’s docs seem to show both signatures depending where you look on the page:
urls(for: in:) and urls( for directory: in domainMask:)

Is this a mistake in their docs?

https://developer.apple.com/reference/foundation/filemanager/1407726-urls

I guess I’m no longer clear on Swift3 method syntax for internal & external names.
Can somebody please clarify? Thanks.

urls(for: in:) and urls( for directory: in domainMask:) are the same thing. However, the second one is used only by Apple in the source code of FileManager. When you want to use this method you should use the first one, without the internal labels.

By definition, FileManager.default.urls(for: in:) returns an array of urls. However, tested with .allDomainMasks but still just returned an array of one url. When would it actually return multiple urls? Thanks.

I don’t think it returns more than one entry on iOS but it can on macOS (at least for some of the options).

Many thanks. That clarifies it.