stringByAppendingPathComponent is unavailable
Solution 1:
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")
Solution 2:
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)
}
}
Solution 3:
Simply wrap your string as NSString
.
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")
Solution 4:
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)