stringByAppendingPathComponent不可用

敢说敢作敢为, 无怨无恨无悔。这篇文章主要讲述stringByAppendingPathComponent不可用相关的知识,希望能为你提供帮助。
我的应用程序在Instagram上分享照片,为此,首先将其保存在临时目录中:

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

它正在研究Swift 1.2,但不适用于Swift 2.0
给出错误消息是:
stringByAppendingPathComponent不可用:改为使用NSURL上的URLByAppendingPathComponent。
答案看起来在Swift 2.0中删除了stringByAppendingPathComponent方法,因此错误消息建议使用:
let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("instagram.igo")

更新:
URLByAppendingPathComponent()已被appendingPathComponent()取代,所以改为:
let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")

另一答案斯威夫特4
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) } }

另一答案它适用于NSString所以你可以像这样使用它:
extension String { func stringByAppendingPathComponent(path: String) -> String { let nsSt = self as NSString return nsSt.stringByAppendingPathComponent(path) } }

现在你可以使用这个扩展,它首先将你的String转换为NSString然后执行操作。
你的代码将是:
let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("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).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) } }

参考HERE。
对于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) } }

另一答案只需将您的字符串换成NSString即可。
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.igo")

另一答案对于Swift 3:
let writePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(directoryname).path

或者更好地创建此扩展:
extension String { func appendingPathComponent(_ string: String) -> String { return URL(fileURLWithPath: self).appendingPathComponent(string).path } }

用法:
let writePath = NSTemporaryDirectory().appendingPathComponent(directoryname)

另一答案Swift 3解决方案:
这是一个获取文档目录路径的函数 -
func getDocumentsDirectory() -> URL { let paths = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask) let documentsDirectory = paths[0] return documentsDirectory }

如何使用:
getDocumentsDirectory.appendingPathComponent("google.com")

结果:
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 2.0
// Get the documents Directory func documentsDirectory() -> String { let documentsFolderPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] return documentsFolderPath }// Get path for a file in the directory func fileInDocumentsDirectory(filename: String) -> String {let writePath = (documentsDirectory() as NSString).stringByAppendingPathComponent("Mobile")if (!NSFileManager.defaultManager().fileExistsAtPath(writePath)) { do { try NSFileManager.defaultManager().createDirectoryAtPath(writePath, withIntermediateDirectories: false, attributes: nil) } catch let error as NSError { print(error.localizedDescription); } } return (writePath as NSString).stringByAppendingPathComponent(filename) }//# MARK: - Save Image in Doc dir func saveImage (image: UIImage, path: String ) -> Bool{let pngImageData = https://www.songbingjia.com/android/UIImagePNGRepresentation(image) //let jpgImageData = UIImageJPEGRepresentation(image, 1.0)// if you want to save as JPEG let result = pngImageData!.writeToFile(path, atomically: true)print("(result)") print("(path)")return result}

另一答案您可以改用URLByAppendingPathComponent()。请注意,您应修剪路径字符串以删除“file://”前缀:
let uniqueFileName = NSUUID().UUIDString let documentsDirectory = getDocumentsDirectoryURL() if let path = documentsDirectory?.URLByAppendingPathComponent(uniqueFileName) { var pathString = path.absoluteString pathString = imagePathString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "file://")) }func getDocumentsDirectoryURL() -> NSURL? { let fileManager = NSFileManager() if let docsDirectory = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first { return docsDirectory } return nil }

另一答案请执行下列操作:
(("(fileName)" as NSString).lastPathComponent as NSString).stringByDeletingPathExtension

另一答案【stringByAppendingPathComponent不可用】如果使用NSString路径方法(而不是String URL方法)是可以接受的,那么使用计算属性或将其值返回

    推荐阅读