O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving and NSKeyedArchiver

Now that you have taught your Employee object to encode and decode itself, you are now ready to add saving and loading to your application. When it is time to save your employees to a file, your Document class will be asked to create an instance of NSData. Once your object has created and returned an NSData object, it will be automatically written to a file.

To create an NSData object, you will use the NSKeyedArchiver class. NSKeyedArchiver has the following class method:

c​l​a​s​s​ ​f​u​n​c​ ​a​r​c​h​i​v​e​d​D​a​t​a​W​i​t​h​R​o​o​t​O​b​j​e​c​t​(​r​o​o​t​O​b​j​e​c​t​:​ ​A​n​y​O​b​j​e​c​t​)​ ​-​>​ ​N​S​D​a​t​a​

This method archives the objects into the NSData object’s buffer of bytes.

Once again, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required