URLs and File Paths
Locate files using URL-based APIs.
URLs and File Paths is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
URLs and File Paths
On Apple platforms files are addressed with URL values, not bare strings. A file URL points at a location in the file system.
File URLs
Create a file URL with URL(fileURLWithPath:). It marks the URL as a local file rather than a network address.
import Foundation
let url = URL(fileURLWithPath: "/tmp/data.txt")
print(url.path)
print(url.isFileURL)Appending Path Components
appendingPathComponent(_:) builds a child path safely, inserting separators for you.
import Foundation
let base = URL(fileURLWithPath: "/tmp")
let file = base.appendingPathComponent("notes.txt")
print(file.path)Path Extensions
Use appendingPathExtension(_:) to add a file type, and pathExtension to read it.
import Foundation
let url = URL(fileURLWithPath: "/tmp/report")
.appendingPathExtension("pdf")
print(url.lastPathComponent)
print(url.pathExtension)Last Path Component
lastPathComponent returns the file or folder name; deletingLastPathComponent() climbs to the parent directory.
import Foundation
let url = URL(fileURLWithPath: "/Users/me/file.txt")
print(url.lastPathComponent)
print(url.deletingLastPathComponent().path)Deleting Extensions
deletingPathExtension() strips the type, useful for deriving related filenames.
import Foundation
let url = URL(fileURLWithPath: "/tmp/image.png")
let base = url.deletingPathExtension()
print(base.lastPathComponent)Modern URL Accessors
Newer APIs use url.appending(path:) and url.path(); both produce equivalent file URLs.
import Foundation
let base = URL(fileURLWithPath: "/tmp")
let child = base.appending(path: "logs/today.log")
print(child.path)URL vs String Path
The path property converts a URL to a plain string when an API needs one. Prefer URLs everywhere else.
import Foundation
let url = URL(fileURLWithPath: "/var/data")
let asString: String = url.path
print(asString)Relative URLs
You can create a URL relative to a base, which is handy for composing paths from a known root directory.
import Foundation
let base = URL(fileURLWithPath: "/tmp/", isDirectory: true)
let rel = URL(fileURLWithPath: "sub/file.txt", relativeTo: base)
print(rel.absoluteURL.path)Standardizing Paths
standardizedFileURL resolves things like .. and . to a clean canonical path.
import Foundation
let messy = URL(fileURLWithPath: "/tmp/a/../b/file.txt")
print(messy.standardizedFileURL.path)Why URLs Not Strings
URLs encode whether a path is a directory, handle separators, and integrate with FileManager. String paths are error-prone by comparison.
import Foundation
let dir = URL(fileURLWithPath: "/tmp/folder", isDirectory: true)
print("Has directory path: \(dir.hasDirectoryPath)")Quick Check
Which method safely builds a child path on a file URL?
Recap
Files are addressed with URL via URL(fileURLWithPath:). You build paths with appendingPathComponent and appendingPathExtension, navigate with lastPathComponent and deletingLastPathComponent, and standardize URLs. Next: reading and writing actual data.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “URLs and File Paths” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “URLs and File Paths”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “URLs and File Paths”?
Locate files using URL-based APIs. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “URLs and File Paths” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- URLs and File Paths
- Reading and Writing Data
- Managing Directories
- App Sandbox Directories