stringByAppendingPathComponent不可用

我的应用程序在Instagram上分享照片,首先将其保存在临时目录中:

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

它正在使用Swift 1.2 ,但在Swift 2.0上不起作用。

鉴于错误信息是:

stringByAppendingPathComponent不可用:改为在NSURL上使用URLByAppendingPathComponent。

它看起来像在Swift 2.0中删除方法stringByAppendingPathComponent ,所以错误消息build议使用:

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

更新:

URLByAppendingPathComponent()已被URLByAppendingPathComponent()replace,所以做:

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

它正在为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) } } 

从这里引用。

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

简单地把你的stringNSString

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

对于Swift 3

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

或者更好的创build这个扩展:

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

用法:

  let writePath = NSTemporaryDirectory().appendingPathComponent(directoryname) 

为迅速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 = 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 } 

Swift 3解决scheme:

这是一个获取文档目录path的函数,

  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 

您可以使用URLByAppendingPathComponent()。 请注意,您应该修剪pathstring以删除“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