Brian Bird
Brian Bird

Reputation: 1206

resolve Ambiguous use of appendingPathComponent error

The code below worked great in an app I published and updated multiple times using swift 2.2. I just migrated over to swift 3 and now I get the following compile time error; "Ambiguous use of appendingPathComponent" with the line:

 let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)

In this:

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

This code is responsible for returning the file path to the documentDirectory that will be used to save a PDF file when a user clicks a review and save PDF button. Any suggestions would be greatly appreciated.

Upvotes: 4

Views: 4491

Answers (1)

rmaddy
rmaddy

Reputation: 318824

appendingPathComponent method is a method of NSString, not AnyObject.

Change this line:

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

to:

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

But you should try to use the appropriate types as much as possible.

Try this:

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

    return PDFPathFileName
}

This code assumes that path has at least one value (which is should).

Upvotes: 8

Related Questions