stringByAppendingPathComponent is unavailable

appendingpathcomponent swift 4
'appendingpathcomponent' is unavailable: use appendingpathcomponent on url instead.
appendingpathcomponent swift 5
swift join path components
swift string file path
swift path to string
working with paths in swift
nsstring append path

My app shares photo on Instagram, to do this it first saves it on a temporary directory:

let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")

It was working on Swift 1.2, but does not work on Swift 2.0.

Given error message is:

stringByAppendingPathComponent is unavailable: use URLByAppendingPathComponent on NSURL instead.

It looks like the method stringByAppendingPathComponent is removed in Swift 2.0, so what the error message suggests is to use:

let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("instagram.igo")

Update:

URLByAppendingPathComponent() has been replaced by appendingPathComponent() so instead do:

let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")

stringByAppendingPathComponent is unavailable, Given error message is: stringByAppendingPathComponent is unavailable: use URLByAppendingPathComponent on NSURL instead. 'stringByAppendingPathComponent' is unavailable: Use 'stringByAppendingPathComponent' on NSString instead. This apparently worked for people before, but it doesn't work for me now in Xcode 7 beta 5. This thread on the Apple Developer Forums had the suggestion to use an extension or do a direct cast to NSString.

It is working for NSString so you can use it like this:

extension String {
    func stringByAppendingPathComponent(path: String) -> String {
        let nsSt = self as NSString
        return nsSt.stringByAppendingPathComponent(path)
    }
}

Now you can use this extension which will convert your String to NSString first and then perform operation.

And your code will be:

let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")

Here is some another methods for use:

extension String {  

    var lastPathComponent: String {  
        return (self as NSString).lastPathComponent  
    }  
    var pathExtension: String {  
        return (self as NSString).pathExtension  
    }  
    var stringByDeletingLastPathComponent: String {  
        return (self as NSString).stringByDeletingLastPathComponent  
    }  
    var stringByDeletingPathExtension: String {  
        return (self as NSString).stringByDeletingPathExtension  
    }  
    var pathComponents: [String] {  
        return (self as NSString).pathComponents  
    }  
    func stringByAppendingPathComponent(path: String) -> String {  
        let nsSt = self as NSString  
        return nsSt.stringByAppendingPathComponent(path)  
    }  
    func stringByAppendingPathExtension(ext: String) -> String? {  
        let nsSt = self as NSString  
        return nsSt.stringByAppendingPathExtension(ext)  
    }  
}

Reference from HERE.

For swift 3.0:

extension String {
    func stringByAppendingPathComponent1(path: String) -> String {
        let nsSt = self as NSString
        return nsSt.appendingPathComponent(path)
    }
}

let writePath = NSTemporaryDirectory().stringByAppendingPathComponent(path: "instagram.igo")


extension String {

    var lastPathComponent: String {
        return (self as NSString).lastPathComponent
    }
    var pathExtension: String {
        return (self as NSString).pathExtension
    }
    var stringByDeletingLastPathComponent: String {
        return (self as NSString).deletingLastPathComponent
    }
    var stringByDeletingPathExtension: String {
        return (self as NSString).deletingPathExtension
    }
    var pathComponents: [String] {
        return (self as NSString).pathComponents
    }
    func stringByAppendingPathComponent(path: String) -> String {
        let nsSt = self as NSString
        return nsSt.appendingPathComponent(path)
    }
    func stringByAppendingPathExtension(ext: String) -> String? {
        let nsSt = self as NSString
        return nsSt.appendingPathExtension(ext)
    }
}

No more stringByAppendingPathComponent in Xcode |Apple , I may have missed it in the release notes, but the Xcode 7 beta 5 compiler is complaining that stringByAppendingPathComponent is not  stringByAppendingPathComponent is unavailable #2. genaks opened this issue Sep 12, 2015 · 1 comment Comments. Copy link Quote reply genaks commented Sep 12, 2015

Simply wrap your string as NSString.

let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")

'stringByAppendingPathComponent' is unavailable, I'm getting the error 'stringByAppendingPathComponent' is unavailable: Use '​stringByAppendingPathComponent' on NSString instead. when I try to do let  Trying to update stringByAppendingPathComponent in Swift 2.0. So basically in swift 1.2, This code was working fine: The problem xcode 7 is giving me is: " 'stringByAppendingPathComponent' is unavailable: Use URLByAppendingPathComponent on NSURL instead.".

for Swift 3:

let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(directoryname).path

or better create this extension:

extension String {
    func appendingPathComponent(_ string: String) -> String {
        return URL(fileURLWithPath: self).appendingPathComponent(string).path
    }
}

usage:

 let writePath = NSTemporaryDirectory().appendingPathComponent(directoryname)

stringByAppendingPathComponent is unavailable, stringByAppendingPathComponent is unavailable. My app shares photo on Instagram, to do this it first saves it on a temporary directory: let writePath  error: 'stringByAppendingPathComponent' is unavailable: Use stringByAppendingPathComponent on NSString instead. error: 'lastPathComponent' is unavailable: Use lastPathComponent on NSString instead. I thought NSString methods were all suppose to be bridged into Swift strings

Swift 3 Solution:

Here is a function to get the documents directory path-

    func getDocumentsDirectory() -> URL {
         let paths = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask)
         let documentsDirectory = paths[0]
         return documentsDirectory
     }

How to use:

    getDocumentsDirectory.appendingPathComponent("google.com")

Result:

    file:///var/folders/w1/3rcp2fvs1qv43hfsh5876s0h0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-7CF9F706-509C-4D4C-997E-AB8FE9E4A6EA/Documents/google.com

Swift: Strings and Paths - Swift Curriculum, on NSString being deprecated and unavailable to Swift. The method is stringByAppendingPathComponent and while it is common to append  'stringByAppendingPathComponent' is unavailable: Use 'stringByAppendingPathComponent' on NSString instead. しようとすると . let documentsFolder = NSSearchPathForDirectoriesInDomains (. DocumentDirectory,. UserDomainMask, true)[0] as String let databasePath = documentsFolder. stringByAppendingPathComponent ("test.sqlite")

Convert iTerm2 color scheme to Terminal.app color scheme. To use, just save to disk and run it like you would any script. NOTE: Needs Xcode 8.0 or later, since it has been ported to Swift 3.0. - iT

Comments
  • if you are going to use this design that you will have problems like convert space to %20 Application%20Support
  • no, Swift 2.0 can use stringByAppendingPathComponent, see my answer below.
  • @JeffreyNeo yes, but that is not a NSURL method but an NSString
  • @DánielNagy I means you said "stringByAppendingPathComponent is removed in Swift 2.0" is not correct, and @Maysam didn't ask for only NSURL method.
  • @JeffreyNeo actually, it is correct, since in Swift 1.2's String had a method called stringByAppendingPathComponent, but Swift 2.0's String doesn't. And NSString is not part of the Swift language, it's part of the Foundation framework.
  • While this is a valid solution, there is a reason why Apple has removed those methods - using paths to locating resources is deprecated and NSURLs should be used instead. Just saying.
  • snippet: String( NSString( string: path ).stringByAppendingPathComponent( imageName ) ) ... otherwise, quite agreed with @CharlieMonroe
  • @CharlieMonroe if that's really the case why are there still a bunch of methods that don't accept an URL as a path, in the SDK?
  • @JorisMans These are usually older methods (available since 10.0, or early after). Ever since sandboxing was introduced, there is no way of passing on a path with e.g. appscope bookmark - you need a URL instead. Apple is slow in updating APIs that only handful people use. Or do you have an example of a recently added API (the last 3-4 years)?
  • @IulianOnofrei - Because you should be using checkResourceIsReachable() or checkPromisedItemIsReachable() on URL instead. FileManager is still an ObjC class NSFileManager with the NS prefix removed for Swift and fileExistsAtPath was there ever since OS X 10.0. The world has evolved since and as apps are sandboxed (which is less obvious on iOS), the file may exist, you just may not have permission to view it; also, the file may be in the cloud, etc. Which is why the simple BOOL method is replaced with something more complex for URL, but more semantically correct.
  • cool one.. String class dont have this but NSString exists! makes sense.