fileExistsAtPath: returning NO for files that exist

前端 未结 3 2106
长情又很酷
长情又很酷 2021-02-11 11:43

At a point in my code fileExistsAtPath: is returning NO for files that I have confirmed exist. I\'ve been scratching my head at this and can\'t figure out why its not working, s

3条回答
  •  灰色年华
    2021-02-11 12:43

    There is a distinction between path and url. A path in unix representation is the location where your particular file or directory exists. Like "/Users/username/Desktop/myfile.txt"

    Wheres a url not only contains the location but scheme as well, e.g: http:// or https:// and in our case a file url (file:///Users/username/Desktop/myfile.txt)

    Methods like fileExistsAtPath or removeItemAtPath need path in the parameter rather than a url.

    While methods like copyItemAtURL:toURL:error:(NSError * _Nullable *)error expects a url.

    Use path attribute of NSURL object to retrieve the path. absoluteString gives you the path along with the scheme.

提交回复
热议问题