PDA

View Full Version : methods




jamesapp
Apr 16, 2008, 12:23 PM
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.


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


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


-(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.


-(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
Apr 16, 2008, 01:19 PM
What you have written there are implementation methods.

So you'd have something like the following:

File: AddressCard.h

#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

#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
Apr 16, 2008, 02:07 PM
-(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:-(void) setName: (NSString *) theName {
if (name != theName) {
[name release];
name = [theName copy];
}
}

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

Spike099
Apr 16, 2008, 02:21 PM
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.