URL with Foundation
TL;DR
種類 | 例 | absoluteURL |
---|---|---|
絶対URL | URL(string:"scheme://example.com") |
scheme://example.com |
相対URL | URL(string:"path/index.html" relativeTo:absURL) |
scheme:example.com/path/index.html |
絶対パス | URL(fileURLWithPath:"/root/path" isDirectory:true) |
file:///root/path/ |
相対パス | URL(fileURLWithPath:"path/test.txt , relativeTo: absFileURL)` |
file:///root/path/test.txt |
NSURL / URL 概要
- インターネットURLとローカルファイルシステム系のURLを表せる
- それぞれに絶対参照と相対参照を持つ
インターネットURL
絶対URL
let absURL = URL(string:"http://example.com")
相対URL
どちらも可
let relativeURL = URL(string:"path/index.html", relativeTo:absURL)
let relativeURL = URL(string:"/path/index.html", relativeTo:absURL)
ローカルファイルパス
絶対パス
let absFileURL = URL(fileURLWithPath:"/root/path", isDirectory:true)
/
を先頭につける
相対パス
let relFileURL = URL(fileURLWithPath:"path/memo.txt", relativeTo:absFileURL)
/
を先頭に付けないrelativeTo
を省略した場合は、カレントの作業ディレクトリが親URLとなる