Search code examples
iosswiftnsfilemanagernskeyedarchiver

Swift NSKeyedArchiver toFile: Unresolved Identifier


As I'm trying to get my NSCoding code to work, saving an array of custom objects, the 'path' saids its unresolved. I've tried to use it as a string "path" but the method is not supposed to take it as a string. What am I doing wrong?

Error:

Use of unresolved identifier 'path'

// After Updating Table, Save Arrays
var success = false

success = NSKeyedArchiver.archiveRootObject(mainArray, toFile: path)

if success {
    print("Saved Blogs")
} else  {
    print("Didn't Save Blogs")
}

ViewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    // NSCoding
    var path : String {
        let manager = FileManager.default
        let url = manager.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
        return url.appendingPathComponent("blogs.plist")!.path
    }
}

Solution

  • Make sure you define your variable inside the same scope as you are calling the method.