methods

Discussion in 'Mac Programming' started by jamesapp, Apr 16, 2008.

  1. macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    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?
     
  2. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #2
    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.
     
  3. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #3
    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];
    }
     
  4. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #4
    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.
     

Share This Page