0

I have updated Xcode to 7 all my apps, after the conversion from swift to swift 2.0, got errors that after a bit of research I was able to manually fix them.

However, I have an error with 'stringByAppendingPathComponent' that I cannot solve. It seems that swift 2.0 removed this method.

The picture attached shows the error I am tiling about.

enter image description here

Any Idea how to solve this issue?

func loadNotes(){


        let plistPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
        let DocumentsDirectory = plistPath[0] as! String
        let path = DocumentsDirectory.stringByAppendingPathComponent("notes.plist")
        let fileManager = NSFileManager.defaultManager()

        if (!fileManager.fileExistsAtPath(path)) {

            if let bundlePath = NSBundle.mainBundle().pathForResource("notes", ofType: "plist") {

                let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
                println("Bundle notes.plist file is --> \(resultDictionary?.description)")
                fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)
                println("copy")

            } else {
                println("notes.plist not found")
            }


        }else {
            println("note.plist already exists")

            //fileManager.removeItemAtPath(path, error: nil)
        }

        let resultDictionary = NSMutableDictionary(contentsOfFile: path)
        println("Loaded notes.plist file is --> \(resultDictionary?.description)")
        var myDict = NSDictionary(contentsOfFile: path)

        if let dict = myDict {
            //load values

        } else {

            println("worning ccould not create dictionary from notes.plist, default values will be used")
        }

    }
SNos
  • 3,430
  • 5
  • 42
  • 92

1 Answers1

-1

Apple wants you to use URLs instead of paths so they have made it unavailable in Swift 2. If you don't want to use NSURL, you can temporarily cast it to NSString instead:

let path = (DocumentsDirectory as NSString).stringByAppendingPathComponent("notes.plist")
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Never convert String to NSString. See what is prescribed by Apple. https://github.com/apple/swift/blob/adc54c8a4d13fbebfeb68244bac401ef2528d6d0/stdlib/public/SDK/Foundation/NSStringAPI.swift#L1345 – Brennan Mar 10 '17 at 17:30