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
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.
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'.
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.
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.
Run
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.
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.