Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
i am reading from a book on Objective-C by Stephen G. Kochan.
there is an address book program from the book that i am working on.
i got the one program to work which i called 15.9 (it displays a person's name and email address) the next program from the book adds methods to program 15.9.

one of the methods deals with overriding the dealloc method.

Code:
-(void) dealloc
{
  [name release];
  [email release];
  [super dealloc];
}

another method deals with releasing the memory from the old email and old name methods.

Code:
-(void) setName: (NSString *) theName
{
  [name release];
  name = [[NSString alloc] initWithString: theName];
}

-(void) setEmail: (NSString *) theEmail
{
  [email release];
  email = [[NSString alloc] initWithString: theEmail];
}

and finally a method to set the name and email fields with one call.

Code:
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
  [self setName: theName];
  [self setEmail: theEmail];
}

just wondering where i should put the new methods. like how do i know what file (implementation, interface, or both,) to put these knew methods into?
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
What you have written there are implementation methods.

So you'd have something like the following:

File: AddressCard.h
Code:
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>

@interface AddressCard:NSObject {
     // Property Declarations
     NSString *name;
     NSString *email;
}

// We are still in the interface, we have not specified @end yet
// Now you want to declare your methods here which will later be defined in
// the implementation file

- (void) dealloc;

- (void) setName:(NSString*) theName;
- (void) setEmail:(NSString*) theEmail;
- (void) setName: (NSString *) theName andEmail: (NSString *) theEmail;

@end // this is the end of the interface file

And now the AddressCard.m implementation
Code:
#import "AddressCard.h"

@implementation

// now, the implementation to your methods are pasted below
-(void) dealloc
{
  [name release];
  [email release];
  [super dealloc];
}

-(void) setName: (NSString *) theName
{
  [name release];
  name = [[NSString alloc] initWithString: theName];
}

-(void) setEmail: (NSString *) theEmail
{
  [email release];
  email = [[NSString alloc] initWithString: theEmail];
}

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
  [self setName: theName];
  [self setEmail: theEmail];
}

@end

Hope this helps.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
Code:
-(void) setName: (NSString *) theName
{
  [name release];
  name = [[NSString alloc] initWithString: theName];
}
Maybe off-topic, but this is a dangerous way to write accessors, because if name happens to point to the same object as theName, you'll release it and then be assigning an invalid object in the next line. You should check to make sure they're not the same first (and if they are the same you don't need to do anything). You can also use the copy method instead of writing out the initilization of a new string (although maybe that's something Apple added to Objective-C that wouldn't be in your book). I write my accessors something like this:
Code:
-(void) setName: (NSString *) theName {
	if (name != theName) {
		[name release];
		name = [theName copy];
	}
}

Or, alternately:
Code:
-(void) setName: (NSString *) theName {
	if (name == theName) {
		return;
	}
	[name release];
	name = [theName copy];
}
 

Spike099

macrumors regular
Feb 18, 2007
143
0
Canada
Good point HiRez. The book was written for Obj-C 1.0. If I were writting accessor methods, i'd synthesize them. Saves so much time and looks so much cleaner. If your currently comfortable writing getter/setters the traditional way, i'd strongly suggest learning how to use synthesizing.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.