2

I converted my Swift to the current Swift syntax and now get the following error in this code:

let paths = NSSearchPathForDirectoriesInDomains(
                FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentsDirectory: AnyObject = paths[0] as AnyObject
let dataPath = documentsDirectory.appendingPathComponent(saveFileName)

Error: Ambiguous use of appendingPathComponent

1 Answer 1

3

Basically

  • never cast a type up to something more unspecific.
  • never cast a distinct known type at all.

NSSearchPathForDirectoriesInDomains returns clearly [String], so delete the annotation and the cast.

let documentsDirectory = paths[0] 

However it's recommended to use the URL related API because in Swift 3 the path functions in String have been removed.

let documentsDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let dataURL = documentsDirectory.appendingPathComponent(saveFileName)

If you really need the string path add

let dataPath = dataURL.path
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. Now I get the following error: appendingPathComponent is unavailable: Use appendingPathComponent on URL instead
Works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.