微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift3 – 解决对appendingPathComponent错误的模糊使用

以下代码在我使用 swift 2.2发布和更新的应用程序中运行良好.我只是迁移到swift 3,现在我得到以下编译时错误; “使用appendingPathComponent”与行的模糊使用:

let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)

在这

func returnPDFPath() -> String {
      let path:NSArray =         NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true) as NSArray
      let documentDirectory: AnyObject = path.object(at: 0) as AnyObject
      let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)

      return PDFPathFileName
 }

 @IBAction func reveiwPDFSendCliked(_ sender: AnyObject) {

    let pdfPathWithFileName = returnPDFPath()

    generatePDFs(pdfPathWithFileName)
 }

代码负责将文件路径返回到documentDirectory,该文件路径将在用户单击查看并保存PDF按钮时用于保存PDF文件.任何建议将不胜感激.

解决方法

appendingPathComponent方法是Nsstring的一种方法,而不是AnyObject.

改变这一行:

let documentDirectory: AnyObject = path.object(at: 0) as AnyObject

至:

let documentDirectory = path.object(at: 0) as! Nsstring

但是你应该尝试尽可能多地使用适当的类型.

试试这个:

func returnPDFPath() -> String {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,true)
    let documentDirectory = path.first! as Nsstring
    let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)

    return PDFPathFileName
}

代码假定路径至少有一个值(应该是).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐