Convert NSURL to local file path

后端 未结 2 1295
离开以前
离开以前 2020-12-13 03:10

I have an NSURL that looks like this:

file://localhost/Users/myuser/myfile.txt

Is there a standard function to convert it to a local file path looking li

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 03:46

    Use the -[NSURL path] method:

    NSLog(@"%@", myUrl.path);
    

    From the documentation:

    The path of the URL, unescaped with the stringByReplacingPercentEscapesUsingEncoding: method. If the receiver does not conform to RFC 1808, returns nil.

    If this URL object contains a file URL (as determined with isFileURL), the return value of this method is suitable for input into methods of NSFileManager or NSPathUtilities. If the path has a trailing slash it is stripped.

    Per RFC 3986, the leading slash after the authority (host name and port) portion is treated as part of the path.

    Note that you can create such a URL with +[NSURL fileURLWithPath:].

提交回复
热议问题