stringByAppendingPathComponent is unavailable
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The error "stringByAppendingPathComponent is unavailable" occurs in Swift when you call this NSString method on a Swift String. Apple removed stringByAppendingPathComponent from the Swift String API because path manipulation belongs to URL and file-system APIs, not string manipulation. The replacement is URL.appendingPathComponent() or NSString.appendingPathComponent (via bridging). This article covers the migration path and modern alternatives.
The Error
This was removed in Swift 2 (Xcode 7). The method still exists on NSString in Objective-C, but Swift String no longer exposes it.
Fix 1: URL.appendingPathComponent() (Recommended)
URL.appendingPathComponent() handles path separators correctly:
Fix 2: NSString Bridging
Cast the Swift String to NSString to access the old method:
This works but is discouraged — Apple wants you to use URL for path manipulation.
Fix 3: String Concatenation (Not Recommended)
String concatenation does not handle edge cases. Always use URL or NSString bridging.
Common Path Operations with URL
FileManager Path Operations
Migration Table
| Old (NSString) | New (URL / Swift) |
stringByAppendingPathComponent: | url.appendingPathComponent() |
stringByDeletingLastPathComponent | url.deletingLastPathComponent() |
stringByAppendingPathExtension: | url.appendingPathExtension() |
stringByDeletingPathExtension | url.deletingPathExtension() |
lastPathComponent | url.lastPathComponent |
pathExtension | url.pathExtension |
pathComponents | url.pathComponents |
stringByExpandingTildeInPath | NSString(string: path).expandingTildeInPath |
Objective-C: Still Works
In Objective-C, stringByAppendingPathComponent: is still available:
The method was never removed from NSString — it was only removed from the Swift String overlay.
String Extension for Convenience
If you have a large codebase and need a quick migration path:
This bridges to NSString internally. Prefer URL for new code but this eases migration.
Common Pitfalls
- Using string concatenation for paths:
base + "/" + filedoes not handle trailing slashes, empty components, or special characters. Always useURL.appendingPathComponent()which normalizes the path. - Confusing file URLs and path strings:
URL(fileURLWithPath:)creates afile://URL.URL(string:)creates a generic URL. UsefileURLWithPathfor local file paths and.pathto convert back to a string. - Assuming
URL.pathand the original string are identical:URL(fileURLWithPath: "/tmp/")produces a path of/tmp(trailing slash removed). If you need the exact original string, keep it separately. - Not handling spaces and special characters: Path strings with spaces work with
URL(fileURLWithPath:)but fail withURL(string:)(which expects percent-encoding). Always usefileURLWithPathfor filesystem paths. - Using
NSStringbridging in new Swift code: While(path as NSString).appendingPathComponent()works, it bypasses Swift's type safety and URL-based design. UseURLfor new code to benefit from the full API (exists checks, resource values, security-scoped access).
Summary
stringByAppendingPathComponentwas removed from Swift'sString— useURL.appendingPathComponent()instead- Create URLs with
URL(fileURLWithPath:)for local paths and use.pathto get the string back - For quick migration, cast to
NSString:(path as NSString).appendingPathComponent(component) - All
NSStringpath methods haveURLequivalents — prefer the URL versions in new code - The method still works in Objective-C and via
NSStringbridging in Swift - Use
FileManager.default.urls(for:in:)for standard directories (Documents, Application Support, Caches)
Related reading
- stringByAppendingPathComponent is unavailable
- structure vs class in swift language
- Styling input buttons for iPad and iPhone
- Subclass UIApplication with Swift
- Submit to App Store issues Unsupported Architecture x86
- Super slow lag/delay on initial keyboard animation of UITextField
- Suppressing deprecated warnings in Xcode
- Swift3 iOS - How to make UITapGestureRecognizer trigger function
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.