Extending the getcwd example in another thread I would like to list all files in that cwd.
To recall:
var buf = [Int8](repeating: 0, count: 256)
return buf.withUnsafeMutableBufferPointer { buf -> String? in
guard let cwdRef = getcwd(buf.baseAddress!, buf.count) else { return nil }
return String(cString: cwdRef)
}
}
Now with the thus obtained cwd ("/" is printed from the App in iPhone), I would like to
list the directory contents. I grabbed this piece of code from some stackoverflow thread, but it is in so far not usable since it is using some .documentDirectory - Swift language question: what does the .documentDirectory notation mean (leading dot)?
And it deals with URLs.
I would like to pass the directory name to the function and let it print its content.
func listDir(dir: String) {
// Create a FileManager instance
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory,
in:.userDomainMask)[0]
do {
let fileUrls = try fileManager.contentsOfDirectory(at:documentsURL, includingPropertiesForKeys: nil)
// process files
print(fileUrls)
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
}
Addendum: I took a look at iOS file management with FileManager in protocol-oriented Swift 4 â iOS Brain
What is the idea behind using "URL"s all the way instead of "files"?