Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if file exist use swift, rewrite from objective c

Tags:

ios

swift

how to rewrite this objective-c language to swift?

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

regards.

like image 427
user1858725 Avatar asked Dec 14 '22 17:12

user1858725


2 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
}
like image 115
Adeel Ur Rehman Avatar answered Feb 15 '23 23:02

Adeel Ur Rehman


Some years after the question has been asked I recommend to take rewrite literally and use the URL related API

let fileURL = URL(fileURLWithPath:"/Applications/MySample.app")
if let _ = try? fileURL.checkResourceIsReachable()  {
   // file exists
}
like image 32
vadian Avatar answered Feb 15 '23 23:02

vadian