Reputation: 9700
So, using Swift 2.0, it looks like Apple are steering us towards using NSURL
rather than NSString
for paths.
I’m trying to ascertain whether a file exists in the user’s Documents directory in iOS, and I can’t quite piece it together.
If I use the following, Swift 2.0 complains that I shouldn’t use stringByAppendingPathComponent
, and that I should use URLs.
let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let dbPath = documents.stringByAppendingPathComponent(“Whatever.sqlite”)
If I then get the URL, like so:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent("Retrollect.sqlite”)
I then can’t call NSFileManager.defaultManager().fileExistsAtPath
to ascertain whether the file exists.
Is there an equivalent of fileExistsAtPath()
for an NSURL
, to look inside the user’s Documents directory?
Upvotes: 1
Views: 2880
Reputation: 3883
Swift 4 version:
FileManager.default.fileExists(atPath: url.path)
Upvotes: 0
Reputation: 9700
Never mind, one of those "five seconds later" answers.
I can use fileExistsAtPath(theURL.path)
to do this. Checking that the path
is non-nil first, of course!
Upvotes: 5