Swift3 解决appendingPathComponent错误的不明确使用

Swift3 解决appendingPathComponent错误的不明确使用,swift3,xcode8,Swift3,Xcode8,下面的代码在我使用swift 2.2发布并多次更新的应用程序中运行良好。我刚刚迁移到swift 3,现在我得到了以下编译时错误;“appendingPathComponent的用法不明确”和以下行: let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String) 在这方面: func returnPDFPath() -> String { let path:NSArray

下面的代码在我使用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,当用户单击review and save PDF按钮时,该目录将用于保存PDF文件。任何建议都将不胜感激。

appendingPathComponent
方法是
NSString
的方法,而不是
任何对象

更改此行:

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

但是您应该尽可能多地使用适当的类型

试试这个:

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

    return PDFPathFileName
}

此代码假定
path
至少有一个值(应该是)。

任何方法都不是
AnyObject
的方法。要查找具有给定方法的类,请在Xcode中的参考文档中搜索它。
func returnPDFPath() -> String {
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentDirectory = path.first! as NSString
    let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)

    return PDFPathFileName
}