Get the filename from a file path in Swift with lastPathComponent
Codemia Knowledge Hub ·
Turn the path into a file URL with URL(fileURLWithPath:) and read its lastPathComponent. Foundation's URL APIs, including pathExtension and deletingPathExtension(), already handle path separators and common edge cases, so you do not need to split the string yourself.
URL(fileURLWithPath:) and lastPathComponent
This is the idiomatic approach in modern Swift. Use fileURLWithPath:, not URL(string:). URL(string:) is for URL syntax such as https://example.com/file.txt, while a local path such as /Users/mark/file.txt should be handled as a file path.
Filename without the extension
Use deletingPathExtension():
This beats splitting on ., because filenames can contain more than one dot.
Just the extension
pathExtension returns it directly:
Only the final extension is returned. If your app needs to detect compound endings such as tar.gz, define that rule explicitly.
NSString path helpers
Older codebases, especially Objective-C-heavy ones, often use NSString helpers. They still work:
For new Swift code, prefer URL, whose API is designed around file locations.
Trailing slashes and directories
A path ending in a slash may describe a directory:
That is the correct last path component, but it is not a filename. If your function should only accept files, add a check with FileManager.
A reusable helper
A helper also gives you one place to add validation later, for example to distinguish files, directories and invalid input.
Why not split the string on /
You can grab the last segment after splitting:
It works in simple cases, but it states intent less clearly than the Foundation API and gets error-prone with empty input, trailing separators or URL-like strings.
Mistakes to avoid
- Using
URL(string:)for a local file path instead ofURL(fileURLWithPath:). - Splitting the path manually when
lastPathComponentalready exists. - Assuming the last path component is always a file rather than possibly a directory.
- Removing the extension by splitting on
.instead of usingdeletingPathExtension(). - Forgetting that
pathExtensionreturns only the final extension segment.
Related reading
- How to get the hour of the day with Swift?
- How to get the indexpath.row when an element is activated?
- How to get the iPhone's screen width in SwiftUI?
- How to get the name of enumeration value in Swift?
- How to get the name of the running application in iOS
- How to get the Power of some Integer in Swift language?
- How to get the result of OnPostExecute to main activity because AsyncTask is a separate class?
- How to get the screen width and height in iOS?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.