3 回答
TA贡献1815条经验 获得超10个赞
看起来stringByAppendingPathComponent在Swift 2.0中删除了该方法,因此错误消息建议使用:
let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("instagram.igo")
更新:
URLByAppendingPathComponent()已取而代之的appendingPathComponent()是:
let writePath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("instagram.igo")
TA贡献1780条经验 获得超1个赞
它正在工作,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)
}
}
- 3 回答
- 0 关注
- 1894 浏览
添加回答
举报