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

前端 未结 7 2135
一向
一向 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: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?

提交回复
热议问题