What is the appropriateFor parameter for in FileManager.url(for:in:appropriateFor:create:)?
Asked Answered
I

3

9

Apple's instructions for creating a temporary URL are to use FileManager.url(for:in:appropriateFor:create:). The example they give is (rewritten in Swift 3):

let desktopURL = URL(fileURLWithPath: "/Users/Noah/Desktop/")
do {
    let temporaryDirectoryURL = try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: desktopURL, create: true)
} catch {
    // handle error
}

The docs say that the appropriateFor parameter "determines the volume of the returned URL", but I don't understand what that means. What is this parameter for and how should I determine the URL to pass in for it?

Ieyasu answered 2/5, 2017 at 16:57 Comment(6)
Are you using Swift 2 or 3? Your code appears to be Swift 2 but your are quoting Swift 3 documentation and class names.Twyla
I'm using Swift 3. The official documentation I linked is written in Swift 2, but that doesn't really make a difference to the question.Ieyasu
You have it backwards. In Swift 3 you should use URL, not NSURL and FileManager, not NSFileManager. It will be easier to answer your Swift 3 question if you post actual Swift 3 code using the updated APIs.Twyla
The url doesn't matter as long as it points to a fileURL in the volume where you want the temporary url to be locatedVegetation
Thank you. And for the record, the reason why my code was in Swift 2 before was because I was quoting the documentation that I linked. Sorry if I hadn't made that clear.Ieyasu
@LeoDabus That's the global temp directory. This function creates a new subdirectory within itIeyasu
D
2

The URL that you pass in is used to determine on which Volume (on which mounted disk) the temporary directory will be created. I suspect you should pass a URL to a file or folder that would reside on the same volume.

Defalcation answered 2/5, 2017 at 17:24 Comment(1)
You might just want to use FileManager.default.temporaryDirectoryDefalcation
T
0

I Did an experiment to understand it.

let document = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
try? FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: document, create: true)

A directory appears in sandbox of my app: enter image description here So I think appropriateFor is used to determine where a temporary directory will be created

Trishatriskelion answered 25/4, 2018 at 8:20 Comment(0)
J
0

I came across this today, years later, but the docs have are clear on this now. I don't remember that they were before, but here's your answer direct from the documentation:

The file URL used to determine the location of the returned URL.

Only the volume of this parameter is used. This parameter is ignored unless the directory parameter contains the value FileManager.SearchPathDirectory.itemReplacementDirectory and the domain parameter contains the value userDomainMask.

Julianjuliana answered 1/2, 2021 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.