Swift 3.0 FileManager.fileExists(atPath:) always return false

前端 未结 7 2126
一向
一向 2021-02-05 00:38

When I use method .fileExists(atPath:)to judge whether the file is exist in file system, the method always return false to me. I checked the file system and the fil

相关标签:
7条回答
  • 2021-02-05 00:51

    I assume your url is an URL type. If so try this out:

    let filePath = url?.path  // always try to work with URL when accessing Files
    if(FileManager.default.fileExists(atPath: filePath!)){  // just use String when you have to check for existence of your file
        let result = NSData(contentsOf: url!)  // use URL instead of String
    }
    

    Saying enough, you should change your implementation like this:

    if(FileManager.default.fileExists(atPath: (url?.path)!)){  // just use String when you have to check for existence of your file
        let result = NSData(contentsOf: url!)  // use URL instead of String
    }
    

    EDIT: 1

    There is even more better way, you can call it swift-way (:D). You don't have to explicitly check for file existence.

    guard let result = NSData(contentsOf: fileURL) else {
        // No data in your fileURL. So no data is received. Do your task if you got no data
        // Keep in mind that you don't have access to your result here.
        // You can return from here. 
        return
    }
    // You got your data successfully that was in your fileURL location. Do your task with your result.
    // You can have access to your result variable here. You can do further with result constant.
    print(result)
    

    Update for Swift 3.0+ without the Objective-Cish NS prefix:

    do {
        let result = try Data(contentsOf: fileURL)
        print(result)
    } catch {
        print(error)
    }
    
    0 讨论(0)
  • 2021-02-05 00:53
     let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
        var path = paths[0] as String;
        path = path + "/YourFilePath"
        if((NSFileManager.defaultManager().fileExistsAtPath(path))) {
                 let result = NSData(contentsOfFile: filePath!)}
    

    Try the above code and check again

    0 讨论(0)
  • 2021-02-05 00:58

    in swift 3 just in case anyone gets confused like i did, here's the full snippets:

    let str = "file:///Users/martian2049/Library/Developer/CoreSimulator/Devices/67D744AA-6EEC-4AFD-A840-366F4D78A18C/data/Containers/Data/Application/DD96F423-AF9F-4F4D-B370-94ADE7D6D0A5/Documents/72b8b0fb-7f71-7f31-ac9b-f9cc95dfe90d.mp3"
    
    let url = URL(string: str)
    print(url!.path,"\n")
    
    if FileManager.default.fileExists(atPath: url!.path) {
        print("FILE Yes AVAILABLE")
    } else {
        print("FILE NOT AVAILABLE")
    }
    

    this prints

    /Users/martian2049/Library/Developer/CoreSimulator/Devices/67D744AA-6EEC-4AFD-A840-366F4D78A18C/data/Containers/Data/Application/DD96F423-AF9F-4F4D-B370-94ADE7D6D0A5/Documents/72b8b0fb-7f71-7f31-ac9b-f9cc95dfe90d.mp3 
    
    FILE Yes AVAILABLE
    

    notice how the 'file://' got chopped off?

    0 讨论(0)
  • 2021-02-05 01:04

    Just use path instead of absoluteString to remove file://

    FileManager.default.fileExists(atPath: URL.init(string: "your_url")!.path)
    
    0 讨论(0)
  • 2021-02-05 01:10
    1. First, what does your file path looks like? If the path begins with a ~,then it must be expanded with expandingTildeInPath;
    2. Check if the path is inaccessible to your app. iOS App can only visits its sandbox directories.
    0 讨论(0)
  • 2021-02-05 01:11

    I want to share my experience, in case anyone else gets baffled by this.

    Tested on iOS 10-11, Xcode 9.2 and Swift 3.2.

    Short answer: if you save a file path to disk, you may solve by not including the Documents directory in it. Instead, every time you need to retrieve the file with the saved path, get the Documents directory and append the path.


    For an iOS app, I was saving an image to .../Documents/Pictures through the relative URL, let's say url. As the image was saved, a path, let's say url.path, was saved too in a Core Data entity.

    When I later tried retrieving the image through FileManager.default.fileExists(atPath: url.path), it always returned false.

    I was testing the app on my iPhone. It turned out that, for some reason, every time I ran the app from Xcode, the app identifier folder changed!!

    So:

    • App opened from Xcode -> Image saved -> app closed -> app opened from physical device -> fileExists -> TRUE
    • App opened from Xcode -> Image saved -> app closed -> app opened from Xcode -> fileExists -> FALSE

    You can check if this is your case by getting and printing the Document folder path (or URL, it doesn't matter) and comparing it with the saved path (or URL). If you get something like this:

    • /var/mobile/Containers/Data/Application/5D4632AE-C432-4D37-A3F7-ECD05716AD8A/Documents..
    • /var/mobile/Containers/Data/Application/D09904C3-D80D-48EB-ACFB-1E42D878AFA4/Documents..

    you found the issue.

    0 讨论(0)
提交回复
热议问题