0Pricing
Swift Academy · Lesson

URLs and File Paths

Locate files using URL-based APIs.

URLs and File Paths is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “URLs and File Paths” lesson free?

Yes — the full text of “URLs and File Paths” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. URLs and File Paths
  2. Reading and Writing Data
  3. Managing Directories
  4. App Sandbox Directories
← Back to Swift Academy