Swift 2.0 - stringByAppendingPathComponent错误转换

时间:2015-10-16 17:36:44

标签: xcode debugging swift2

我已经将Xcode更新为7我的所有应用程序,在从swift转换为swift 2.0之后,得到了错误,经过一些研究后我能够手动修复它们。

但是,我遇到了' stringByAppendingPathComponent'我无法解决。似乎swift 2.0删除了这种方法。

附上的图片显示了我所抱怨的错误。

enter image description here

任何想法如何解决这个问题?

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

    }

1 个答案:

答案 0 :(得分:-1)

Apple希望您使用网址而不是路径,以便它们在Swift 2中不可用。如果您不想使用NSURL,可以暂时将其转换为NSString

let path = (DocumentsDirectory as NSString).stringByAppendingPathComponent("notes.plist")