stringByAppendingPathComponent is unavailable stringByAppendingPathComponent is unavailable ios ios

stringByAppendingPathComponent is unavailable


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")


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)    }}


Simply wrap your string as NSString.

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