为了账号安全,请及时绑定邮箱和手机立即绑定

stringByAppendingPathComponent不可用

stringByAppendingPathComponent不可用

慕村9548890 2019-09-03 16:00:05
我的应用程序在Instagram上分享照片,为此,首先将其保存在临时目录中:let writePath = NSTemporaryDirectory().stringByAppendingPathComponent("instagram.igo")它正在努力Swift 1.2,但不起作用Swift 2.0。给出错误消息是:stringByAppendingPathComponent不可用:改为使用NSURL上的URLByAppendingPathComponent。
查看完整描述

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")


查看完整回答
反对 回复 2019-09-03
?
慕神8447489

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)

    }

}


查看完整回答
反对 回复 2019-09-03
  • 3 回答
  • 0 关注
  • 1894 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信