Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resolve Ambiguous use of appendingPathComponent error

Tags:

xcode8

swift3

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.

like image 512
Brian Avatar asked Feb 06 '23 00:02

Brian


1 Answers

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

like image 115
rmaddy Avatar answered Feb 21 '23 04:02

rmaddy