Reputation: 6581
I have an extension of String like this:
extension String {
var fileURL: URL {
return URL(fileURLWithPath: self)
}
func appendingPathComponent(_ string: String) -> String {
return fileURL.appendingPathComponent(string).path
}
var lastPathComponent:String {
get {
return fileURL.lastPathComponent
}
}
var deletingPathExtension: String {
return fileURL.deletingPathExtension().path
}
}
Problem is lastPathComponent
contains '/' as opposed to NSString
counterpart which doesn't contains this. What is an elegant workaround? Didn't find an elegant answer in StackOverflow.
EDIT: Here is the real code that gives me issues:
filenameField.text = path.lastPathComponent.deletingPathExtension
I fixed it by switching the two routines, i.e.
filenameField.text = path.deletingPathExtension.lastPathComponent
Upvotes: 5
Views: 9462
Reputation: 5946
I'd suggest to use
(str as NSString).lastPathComponent
You won't create new objects in that case.
Upvotes: 5