Basic class relationship question
Basic class relationship question
- Subject: Basic class relationship question
- From: "ClunkyRobot" <email@hidden>
- Date: Wed, 07 Jul 2004 17:50:21 -0700
I am learning the real basic cocoa/ objective-c I have taken a basic
tutorials on NSTableView displaying a NSMutableDictionary content, which
I basically have working. Now I am trying to Archive the
NSMutableDictionary content basically a bunch of strings.
Now I have 5 classes Controller.h and Controller.m which links the Nib
with the NSMutableDictionary and the NSTableView. Then I have a
KeyArchiver.h and KeyArchiver.m which is my Archiver class. Then I have
a main.m
The question is how do I join the two together Controller and the
KeyArchiver classes?
Now my KeyArchiver class contains a method which sets the values for
the Archiver that will be encoded.
-(void) setName: (NSString *) name setEmail: (NSString *) email ;
Now I exec this code like this in my main:
code:
==== ==== ===== ==== ==== ===== ==== ==== =====
main.m
==== ==== ===== ==== ==== ===== ==== ==== =====
#import <Cocoa/Cocoa.h>
#import "Controller.h"
#import "KeyArchiver.h"
int main(int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
KeyArchiver *myKeyArchiver = [[KeyArchiver alloc] init];
Controller *myController = [[Controller alloc] init];
// First set and archive myKeyArchiver1 to a file
[ myKeyArchiver setName:[myController getNameValue]
setEmail:[myController getEmailValue] ];
[ NSKeyedArchiver archiveRootObject: myKeyArchiver toFile:
@"~/Desktop/AddressBook.data" ];
[ myKeyArchiver release ];
[ pool release ];
return NSApplicationMain(argc, argv);
}
It all compiles fine without error or warning but adding data to my
NSTableView has no affect on my KeyArchiver code in my main. So I
gather it is never being executed or something.
So were do I put my:
[ myKeyArchiver setName:[myController getNameValue]
setEmail:[myController getEmailValue] ];
[ NSKeyedArchiver archiveRootObject: myKeyArchiver toFile:
@"~/Desktop/AddressBook.data" ];
To make it work with my two other classes and the project builder
files.
This Controller class it seems specially for the controller aspect of
the MVC, is this right? So I am really unsure about how to wire this
all together.
Many thanks for any help.
code:
==== ==== ===== ==== ==== ===== ==== ==== =====
KeyArchiver.m
==== ==== ===== ==== ==== ===== ==== ==== =====
#import "KeyArchiver.h"
@implementation KeyArchiver;
-(void) setName: (NSString *) name setEmail: (NSString *) email {
nameValue = name;
emailValue = email;
}
-(NSString *) nameValue { return nameValue; }
-(NSString *) emailValue { return emailValue; }
-(void) encodeWithCoder: (NSCoder *) encoder {
if ( [encoder allowsKeyedCoding] ) {
[encoder encodeObject: nameValue forKey:
@"KeyArchiverNameValue"];
[encoder encodeObject: emailValue forKey:
@"KeyArchiverEmailValue"];
} else {
[encoder encodeObject: nameValue];
[encoder encodeObject: nameValue];
}
}
-(id) initWithCoder: (NSCoder *) decoder {
if ( [decoder allowsKeyedCoding] ) {
nameValue = [[decoder decodeObjectForKey:
@"KeyArchiverNameValue"] retain];
emailValue = [[decoder decodeObjectForKey:
@"KeyArchiverEmailValue"] retain];
} else {
nameValue = [[decoder decodeObject] retain];
emailValue = [[decoder decodeObject] retain];
}
return self;
}
@end
==== ==== ===== ==== ==== ===== ==== ==== =====
Controller.m
==== ==== ===== ==== ==== ===== ==== ==== =====
#import "Controller.h"
@implementation Controller
- (id)init {
records = [[NSMutableArray alloc] init];
return self;
}
- (NSDictionary *)createRecord {
NSMutableDictionary *record = [[NSMutableDictionary alloc] init];
[record setObject:[firstNameField stringValue] forKey:@"First
Name"];
[record setObject:[lastNameField stringValue] forKey:@"Last Name"];
[record setObject:[emailField stringValue] forKey:@"Email"];
[record setObject:[homePhoneField stringValue] forKey:@"Home
Phone"];
[record setObject:[textArea stringValue] forKey:@"Text Area"];
[record autorelease];
return record;
}
-(NSString *) getNameValue { return firstNameField; }
-(NSString *) getEmailValue { return emailField; }
- (IBAction)addRecord:(id)sender {
[records addObject:[self createRecord]];
[tableView reloadData];
}
- (IBAction)deleteRecord:(id)sender {
int status;
NSEnumerator *enumerator;
NSNumber *index;
NSMutableArray *tempArray;
id tempObject;
if ( [tableView numberOfSelectedRows] == 0 )
return;
NSBeep();
status = NSRunAlertPanel(@"Warning!", @"Are you sure that you want
to delete the selected record(s)?", @"OK", @"Cancel", nil);
if ( status == NSAlertDefaultReturn ) {
enumerator = [tableView selectedRowEnumerator];
tempArray = [NSMutableArray array];
while ( (index = [enumerator nextObject]) ) {
tempObject = [records objectAtIndex:[index intValue]];
[tempArray addObject:tempObject];
}
[records removeObjectsInArray:tempArray];
[tableView reloadData];
}
}
- (IBAction)insertRecord:(id)sender {
int index = [tableView selectedRow];
[records insertObject:[self createRecord] atIndex:index];
[tableView reloadData];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
return [records count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex {
id theRecord, theValue;
theRecord = [records objectAtIndex:rowIndex];
theValue = [theRecord objectForKey:[aTableColumn identifier]];
return theValue;
}
@end
==== ==== ===== ==== ==== ===== ==== ==== ====
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.