You can think of NSKeyedArchiver
as just a way of saving an object (serializing) to disk. In your case, that object is an NSDictionary
, that has exactly one key, student
(I'm using a JSON-ish notation to show the data structures; hopefully intent is clear):
"student" : { "studentId" : 25,
"studentName" : "John Smith"
}
As you've noticed, every time you call saveDataToDisk
, it is just overwriting that single "student" key. To save more than one student, there's a couple ways you could go about it: your choice will depend on how you want to read the data back.
One option is to store an array, instead of an NSDictionary
:
"student" : [ { "studentId: 25, "studentName" : "John Smith" },
{ "studentId: 26, "studentName" : "Jane Doe" } ]
If you take this approach, you'll want to make sure that saveDataToDisk
has access to an NSArray
of students, and pass that array to rootObject setValue:
instead:
[rootObject setValue:students forKey:@"students"];
Alternatively, you could imagine wanting to just add more keys to the dictionary, each one representing a student:
"student25" : { "studentId: 25, "studentName" : "John Smith" },
"student26" : { "studentId: 26, "studentName" : "Jane Doe" }
In this case, you would again need to make sure that your save function has access to an array of students, but this time you will loop over that array, and add each one to the rootDictionary with its own key that you construct by (for example) concatenating "student" with the studentId.