ARTICLE AD BOX
I encountered a breaking change in iOS 26 when using URL(fileURLWithPath:) with filenames that start with a tilde (~).
The Problem:
let filename = "\~MyFile.txt" let url = URL(fileURLWithPath: filename) print(url.lastPathComponent)Output:
iOS 18 → ~MyFile.txt ✅ iOS 26 → 924AF0C4-C3CD-417A-9D5F-733FBB8FCF29 ❌On iOS 26, the tilde (~) is being interpreted as the home directory symbol, causing path resolution to the app container. The lastPathComponent then returns the container UUID instead of the original filename.
Debug Output:
iOS 18:
URL created: file:///~MyFile.txt
lastPathComponent: ~MyFile.txt ✅
iOS 26:
URL created: file:///var/mobile/Containers/Data/Application/924AF0C4.../
lastPathComponent: 924AF0C4-C3CD-417A-9D5F-733FBB8FCF29 ❌
Workaround:
Using
NSString.lastPathComponentworks correctly on both iOS versions:
let filename = "\~MyFile.txt" let result = (filename as NSString).lastPathComponent// Returns: "~MyFile.txt" on both iOS 18 and iOS 26
Questions:
1. Is this an intentional change in iOS 26's Foundation framework?
2. Are there any other special characters affected by this change?
3. Is using NSString.lastPathComponent the correct long-term solution?
Tested other characters:
I also tested these characters that could potentially be misinterpreted during path or URL parsing: .. . : @ # ? %
All of them worked correctly. Only ~ at the beginning of the filename causes this issue.
