I found this black of code on NSCoding and it almost does want I want it to. the link for where I found it is below. How do I create a NSCoding class and user in in other classes? The below code dies not work. I hope some can help me with this.
import Foundation
import UIKit
class User: NSObject, NSCoding {
var name: String
init(name: String) {
self.name = name
}
required init(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObjectForKey("name") as String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
}
}
//new class where I want to set and get the object
class MyNewClass: UIViewController {
let user = User(name: "Mike")
let encodedUser = NSKeyedArchiver.archivedDataWithRootObject(user)
let decodedUser = NSKeyedUnarchiver.unarchiveObjectWithData(encodedUser) as User
}
//http://stackoverflow.com/questions/24589933/nskeyedunarchiver-fails-to-decode-a-custom-object-in-swift
I'm cutting and pasting from my own project below. I have limited this to one string parameter to store to file. But you can more of different types. You can paste this into a single swift file and use it as the ViewController plus added classes to test. It demonstrates using NSCoding with swift syntax to save and retrieve data in an object.