Sponsor Link
環境&対象
- macOS15.0.1 Sequoia
- Xcode 16.2 Beta
- iOS 18.1
- Swift 5.9




URL
URL という型には、URI の情報が保持されています。その中には パス の情報が含まれています。(空かも知れませんが)
URL には、このパスを操作するためのメソッドが多く用意されていますので、確認していきます。
参考
URLApple Developer Documentation
パス操作
URL の使い道の1つは、ファイルを指し示すことです。
この記事では、ファイルパスを表す URL について 確認していきます。
HTML中に、半角文字で file:// と書くと、うまくレンダリングできなかったので、コード中では、file:// と全角文字で表現しています。コードで試す時には、適宜 置き換えてください。
パスへの追加・削除
パスに追加したり削除したりするメソッドももちろん用意されています。
ただし、追加/ 削除できるのは、パスの最終要素です。
追加削除のメソッドは、append/ appending のように、似た機能が複数用意されています。
違いは動詞 がそのまま使われているメソッド(例:append) はmutating なメソッドであり 自身を変更し、
-ing になっているメソッド(例:appending) は、操作結果をメソッドの返り値として返すというものです
以降では ing 系(操作結果がメソッド返り値)のメソッドで説明していきます。
追加
1つづつ(?) 追加するメソッドと、複数をまとめて追加するメソッドがあります。
参考
appending(path:directoryHint:)Apple Developer Documentation
参考
appending(components:directoryHint:)Apple Developer Documentation
複数をまとめて追加するメソッドは、可変長引数になっています。
また、少し変わった(?)メソッドとして、拡張子を付与するメソッドも用意されています。
append(path:…) と append(component:…) は、path に含まれる path separator を エンコーディングするかどうかが異なります。component: を使用すると path separator に “\”(バックスラッシュ)を使っていると、”%5c” に変換されます。
appendingPathComponent(_:) 等は、deprecated になっています。
struct URL_FilePath {
@Test func filePath_appending() async throws {
let sut = URL(filePath: "/tmp/Internal/")
#expect(sut.absoluteString == "file:///tmp/Internal/")
#expect(sut.hasDirectoryPath == true)
#expect(sut.appending(component: "More").absoluteString == "file:///tmp/Internal/More")
#expect(sut.appending(component: "More").hasDirectoryPath == false)
#expect(sut.appending(component: "More/").absoluteString == "file:///tmp/Internal/More/")
#expect(sut.appending(component: "More/").hasDirectoryPath == true)
#expect(sut.appending(component: "More.jpg").absoluteString == "file:///tmp/Internal/More.jpg")
#expect(sut.appending(component: "More.jpg").hasDirectoryPath == false)
#expect(sut.appending(components: "More", "MoreMore").absoluteString == "file:///tmp/Internal/More/MoreMore")
#expect(sut.appending(components: "More", "MoreMore").hasDirectoryPath == false)
#expect(sut.appending(component: "More").appendingPathExtension("jpg").absoluteString == "file:///tmp/Internal/More.jpg")
}
percent encoding
追加するときに、パーセント エンコーディングするかどうかで、appending(component:…) か appending(path:…) を使い分けることが必要です。
appending(component:…) は、パーセント エンコーディングしない メソッドです。
パーセント エンコーディングとは、URL として使用している文字のうち 特定の文字が特殊な意味を持つため、必要に応じて 文字を置換することです。
パーセント エンコーディングの例として “&”(アンパサンド)を %26 に変換する があります。
削除
追加と同じように、削除できるのも一番最後の component です。
具体的には lastPathComponent か pathExtension のいずれかを削除することができます。
struct URL_FilePath {
@Test func filePath_deleting() async throws {
let sut = URL(filePath: "/tmp/Internal/test.jpg")
#expect(sut.absoluteString == "file:///tmp/Internal/test.jpg")
#expect(sut.hasDirectoryPath == false)
#expect(sut.deletingLastPathComponent().absoluteString == "file:///tmp/Internal/")
#expect(sut.deletingLastPathComponent().hasDirectoryPath == true)
#expect(sut.deletingPathExtension().absoluteString == "file:///tmp/Internal/test")
#expect(sut.deletingPathExtension().hasDirectoryPath == false)
}
}
まとめ
URL のパスを扱うメソッドを確認しました。
- appending/append でパスを追加できる
- パーセントエンコーディングの必要性に応じてメソッドを使い分ける必要がある
- 削除は、lastPastComponent/pathExtension が対象
説明は以上です。
不明な点やおかしな点ありましたら、こちらまで。
SwiftUI おすすめ本
SwiftUI を理解するには、以下の本がおすすめです。
SwiftUI ViewMatery
SwiftUI で開発していくときに、ViewやLayoutのための適切なmodifierを探すのが大変です。
英語での説明になってしまいますが、以下の”SwiftUI Views Mastery Bundle”という本がビジュアル的に確認して探せるので、便利です。
英語ではありますが、1ページに コードと画面が並んでいるので、非常にわかりやすいです。
View に適用できる modifier もわかりやすく説明されているので、ビューの理解だけではなく、どのような装飾ができるかも簡単にわかります。
超便利です
販売元のページは、こちらです。
SwiftUI 徹底入門
# SwiftUI は、毎年大きく改善されていますので、少し古くなってしまいましたが、いまでも 定番本です。
Swift学習におすすめの本
詳解Swift
Swift の学習には、詳解 Swift という書籍が、おすすめです。
著者は、Swift の初期から書籍を出していますし、Swift の前に主力言語だった Objective-C という言語についても同様の書籍を出しています。
最新版を購入するのがおすすめです。
現時点では、上記の Swift 5 に対応した第5版が最新版です。
Sponsor Link