How to check if a file exists, use swift, rewrite from c object

How to rewrite this objective-c language to fast?

NSString *filePath = @"/Applications/MySample.app";
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        // avoid open add friend
    }

Sincerely.

+4
source share
3 answers

Equivalent Swift 3 Code:

let filePath = "/Applications/MySample.app"
if (FileManager.default.fileExists(atPath: filePath)) {
    // avoid open add friend
}

Swift 2

let filePath = "/Applications/MySample.app"
if (NSFileManager.defaultManager().fileExistsAtPath(filePath))
{
    // avoid open add friend
}
+9
source
let path = "/Applications/MySample.app"
let hasFile = FileManager().fileExists(atPath: path)
if hasFile {
    // use file
}
else {
    // possibly inform user the file does not exist
}
0
source

A few years after the question was asked, I recommend rewriting it literally and using the API associated with the URL

let fileURL = URL(fileURLWithPath:"/Applications/MySample.app")
if let _ = try? fileURL.checkResourceIsReachable()  {
   // file exists
}
0
source

Source: https://habr.com/ru/post/1568582/


All Articles