Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

406
Views
FileManager.default.fileExists says documents directory does not exist

FileManager.default.contentsOfDirectory claims that the documents directory does not exist, even though it clearly does. I'm using Swift 4.2 on my actual iPhone SE running iOS 12.1.2

I am reading the contents of the downloads directory in my app using the following:

do {
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let downloadedContents = try FileManager.default.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
    print(downloadedContents)
} catch {
    print("Error while enumerating contents: \(error.localizedDescription)")
}

This prints the following, telling me that a file exists in the documents directory:

[file:///private/var/mobile/Containers/Data/Application/698F8D51-92AF-4BAB-A212-0A0982090550/Documents/example-file/]

(I moved the file there from the caches directory after downloading an in-app purchase, but I don't think that's relevant to this question).

Later in my code, I want to check if the file was downloaded. I'm using the following:

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let path = URL(fileURLWithPath: "example-file", relativeTo: documentsURL)
var isDir : ObjCBool = false
if FileManager.default.fileExists(atPath: path.standardizedFileURL.absoluteString, isDirectory: &isDir) {
    if isDir.boolValue {
        return true
    } else {
        return false // file exists but is not directory
    }
} else {
    return false // file does not exist at all
}

But this always returns false, even though contentsOfDirectory showed it exists.

While debugging, I also tried:

let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
po FileManager.default.fileExists(atPath: documentsURL.standardizedFileURL.absoluteString)

But this, too, returns false. Now I'm pretty sure I'm just using the fileExists methods incorrectly.

Can anyone spot what I'm doing wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Turns out one should use documentsURL.path, instead of any sort of URL.

The path begins with /var/mobile... whereas the URLs begin with file:///var...

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!