Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)

8: Archiving using NSArchiver

Implement simple code archive using NSArchiver, NSUnarchiver, encodeWithCoder and initWithCoder.

Create a new project in XCode: File->New Project->Cocoa Application
Call it: 008-NSArchiver

Now create the class: MySaveMenuController of type NSObject.
and the class: MyProgData of type NSObject.
Code these as shown below and save.
Note: if you have already done the NSKeyedArchiver example then just duplicate the 007-Save folder and rename the project using Rename Xcode Project

// MySaveMenuController.h // 008-NSArchiver #import <Cocoa/Cocoa.h> #import "MyProgData.h" @interface MySaveMenuController : NSMenu { MyProgData * myMyProgDataObj; } - (IBAction)doTheSave:(id)pId; - (IBAction)doTheLoad:(id)pId; @end // MySaveMenuController.m // 008-NSArchiver #import "MySaveMenuController.h" @implementation MySaveMenuController - (IBAction)doTheSave:(id)pId; { NSData *tvarData = [NSArchiver archivedDataWithRootObject:myMyProgDataObj]; BOOL tvarResult = [tvarData writeToFile:@"../../../008_MySavedFile" atomically:YES]; if (tvarResult) { int tvarInt = [myMyProgDataObj myNumber]; NSLog(@"doTheSave succeded: tvarInt = %3d",tvarInt); } else { NSLog(@"doTheSave failed"); } // end if } // end doTheSave - (IBAction)doTheLoad:(id)pId; { NSError ** tvarErrorPtr = nil; id tvarData = [[NSData alloc] initWithContentsOfFile:@"../../../008_MySavedFile" options:NSUncachedRead error:tvarErrorPtr]; if (tvarErrorPtr) { NSLog(@"doTheLoad error in initWithContentsOfFile"); return; } // end if if (!tvarData) { NSLog(@"doTheLoad tvarData = nil so do an initWithCoder"); myMyProgDataObj = [[MyProgData alloc]initWithCoder:nil]; } else { NSLog(@"doTheLoad tvarData OK = %8d",(int)tvarData); myMyProgDataObj = [NSUnarchiver unarchiveObjectWithData:tvarData]; } // end if if (myMyProgDataObj) { int tvarInt = [myMyProgDataObj myNumber]; NSLog(@"doTheLoad succeded: tvarInt = %3d",tvarInt); } else { NSLog(@"doTheLoad failed - create MyProgData object with initWithCoder"); myMyProgDataObj = [[MyProgData alloc]initWithCoder:nil]; } // end if } // end doTheLoad @end // MyProgData.h // 008-NSArchiver #import <Cocoa/Cocoa.h> @interface MyProgData : NSObject { int myNumber; } - (id)initWithCoder:(NSCoder *)pCoder; - (void)encodeWithCoder:(NSCoder *)pCoder; - (int) myNumber; @end // MyProgData.m // 008-NSArchiver #import "MyProgData.h" @implementation MyProgData - (id)initWithCoder:(NSCoder *)pCoder; { if ((self = [super init]) == nil) { return self; } // end if [pCoder decodeValueOfObjCType:@encode(int) at: &myNumber]; if (myNumber) { myNumber++; } else { myNumber = 1; } // end if return self; }// end initWithCoder - (void)encodeWithCoder:(NSCoder *)pCoder; { [pCoder encodeValueOfObjCType:@encode(int) at: &myNumber]; }// end encodeWithCoder - (int) myNumber { return myNumber; } // end myNumber @end

Bring up Interface Builder by double clicking on MainMenu.nib.

Select the File menu. Inside the Class menu of the Identity panel of the inspector choose MySaveMenuController. You will see the doTheLoad and doTheSave in the actions section.

XCode,Interface Builder - NSMenu with actions to NSArchiver

Interface Builder: NSMenu object, its class and actions

Drag a Menu Item from the IB Library and insert it just above the 'Save' item in the File Menu. Double click on it to select its text and change it from 'Item' to 'Load'.

XCode,Interface Builder - new menu item

Interface Builder: name the new menu item

Click on File to select it.
In the connections panel of the Inspector drag from the round circles of doTheLoad and doTheSave to the Load and Save manu items respectively.

XCode,Interface Builder, link menu items to actions

Interface Builder link menu items to actions

Save Interface Builder
Return to XCode, build and run.
Do File->Load, File-Save and Quit then repeat.

XCode,Interface Builder, run the program

Run

XCode,Interface Builder, console log

Console Log

Double click on the file '007_MySavedFile'
It will open in the Property List Editor.
You will see the saved number in there.

XCode Property List Editor

Result seen inside the Property List Editor

If you want to download the code

Click the Download Link to obtain 008-NSArchiver.zip file of this whole OS X 10.5 Leopard program.

Download 008-NSArchiver.zip (2.3 MB)



Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • link to painting of the Cities of Babel destroying our Garden of Eden

animatedPaint