original source : https://stackoverflow.com/questions/16176911/nsurl-path-vs-absolutestring
What is the actual difference between these methods?
Let’s analyze this writing 6 lines of code – 3 for a local and 3 for http URL – and playing around with them a little bit.
Let’s create an
NSURL using the
file:// scheme. If you ask yourself why there are 3
file: you should remember that a complete URL exists of a scheme (
file:// and absolute or relative path (you can find more information on creating URLs in RFC 1808 on page 3). We use an absolute path which starts with a
/ so that we end up with
NSURL *aLocalURL = [NSURL URLWithString:@"file:///Users/dennis/Desktop/"];
NSLog(@"absolute string: %@", aLocalURL.absoluteString);
NSLog(@"path: %@", aLocalURL.path);
absolute string: file:///Users/dennis/Desktop/
So we see that
absoluteString still knows its scheme whereas
path doesn’t have this information anymore.
path is a file (directory) URL and as the docs state, the trailing slash it is stripped.
Now let’s take a look at remote URLs. With these type of URLs most people are more familiar. We create it using the same procedure as for local URLs. Our scheme is now
http:// and our
NSURL *anHTTPURL = [NSURL URLWithString:@"http://www.apple.com/"];
NSLog(@"absolute string: %@", anHTTPURL.absoluteString);
NSLog(@"path: %@", anHTTPURL.path);
absolute string: http://www.apple.com/
Again, we see that the absolute string still knows its scheme but
path is now
path seems to be not an appropriate way when working with remote URLs.
However, when we have an
absolute string: http://www.apple.com/index.html
Reading the docs helps here, too:
Per RFC 3986, the leading slash after the authority (host name and port) portion is treated as part of the path.
path is everything beginning (and including) at the slash after the
authority which is
www.apple.com in our case.
Is there a time when one should be used over the other?
From the docs: (method:
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.
In my opinion that sentence states clearly that you should use
path when you work with
When you work with remote URLs you (generally) use
absoluteString, otherwise the result is not what you (generally) want.
When you work with local URLs use
NSURL Class Reference
내가 추가로 기입하는 내용
let documentsUrl = FileManager.default.urls(for: .applicationSupportDirectory, in:.userDomainMask).first!
let url = documentsUrl.appendingPathComponent(“QuestionModel.sqlite”)
print (“?? (url.path) ??n”)
print (“++ (url.absoluteString) ++n”)
위와 같은 코드를 이용하여 application support 폴더내 화일에 접근하려는 경우
?? /Users/owner/Library/Developer/CoreSimulator/Devices/7C444728-467E-4085-8E94-6298D30B68D1/data/Containers/Data/Application/055751E5-8656-4B84-AE74-99AB01522080/Library/Application Support/QuestionModel.sqlite ??
++ file:///Users/owner/Library/Developer/CoreSimulator/Devices/7C444728-467E-4085-8E94-6298D30B68D1/data/Containers/Data/Application/055751E5-8656-4B84-AE74-99AB01522080/Library/Application%20Support/QuestionModel.sqlite ++
와 같이 결과가 된다. path의 경우 space가 정상적으로 표시된다. absoluteString의 경우는 %20으로 표현된다.