stringByAppendingPathComponent不可用

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


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

它正在努力Swift 1.2,但不起作用Swift 2.0。


给出错误消息是:


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


慕村9548890
浏览 1895回答 3
3回答

动漫人物

看起来stringByAppendingPathComponent在Swift 2.0中删除了该方法,因此错误消息建议使用:let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("instagram.igo")更新:URLByAppendingPathComponent()已取而代之的appendingPathComponent()是:let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")

慕神8447489

它正在工作,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)    }}
打开App,查看更多内容
随时随地看视频慕课网APP