Loading and NSKeyedUnarchiver

Now you will add the ability to load files to your application. Once again, NSDocument has taken care of most of the details for you.

To do the unarchiving, you will use NSKeyedUnarchiver, which has the following handy method:

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

In Document.swift, edit your readFromData(_:ofType:error:) method to look like this:

o​v​e​r​r​i​d​e​ ​f​u​n​c​ ​r​e​a​d​F​r​o​m​D​a​t​a​(​d​a​t​a​:​ ​N​S​D​a​t​a​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​o​f​T​y​p​e​ ​t​y​p​e​N​a​m​e​:​ ​S​t​r​i​n​g​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​e​r​r​o​r​ ​o​u​t​E​r​r​o​r​:​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.