Files and Paths
Path Components
File URLs can expose the folder and file pieces of a path without opening the file.
Read path pieces
path_components.swift
import Foundation
let path =
let url = URL(fileURLWithPath: path)
let file = url.lastPathComponent
let folder = url.deletingLastPathComponent().lastPathComponent
let message = "\(folder)/\(file)"
print(message)
import Foundation
let path =
let url = URL(fileURLWithPath: path)
let file = url.lastPathComponent
let folder = url.deletingLastPathComponent().lastPathComponent
let message = "\(folder)/\(file)"
print(message)
import Foundation
let path =
let url = URL(fileURLWithPath: path)
let file = url.lastPathComponent
let folder = url.deletingLastPathComponent().lastPathComponent
let message = "\(folder)/\(file)"
print(message)
path components
`lastPathComponent` reads the final path segment, while `deletingLastPathComponent` moves one level up.