Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 16, 2008, 12:23 PM   #1
jamesapp
macrumors 6502
 
Join Date: Mar 2008
methods

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?
jamesapp is offline   0 Reply With Quote
Old Apr 16, 2008, 01:19 PM   #2
Spike099
macrumors regular
 
Join Date: Feb 2007
Location: 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.
__________________
MacBook 4GBRam 320GBHDD
iMac 20" 4GBRam 1TBHDD
iPhone 16GB
Cocoa Developer
Spike099 is offline   0 Reply With Quote
Old Apr 16, 2008, 02:07 PM   #3
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by jamesapp View Post
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];
}
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Apr 16, 2008, 02:21 PM   #4
Spike099
macrumors regular
 
Join Date: Feb 2007
Location: 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.
__________________
MacBook 4GBRam 320GBHDD
iMac 20" 4GBRam 1TBHDD
iPhone 16GB
Cocoa Developer
Spike099 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Calling methods lynkynpark86 iPhone/iPad Programming 2 Feb 20, 2011 01:33 PM
Cydia Error: The method driver /usr/lib/apt/methods/HTTP could not be found GangstaMac Jailbreaks and iOS Hacks 0 Aug 28, 2010 02:22 PM
Calling a method from a method spire.bt iPhone/iPad Programming 2 Aug 26, 2010 07:13 AM
method not fully implement / method not find?‏ inzaghi0507 iPhone/iPad Programming 3 Jul 25, 2010 07:34 AM


All times are GMT -5. The time now is 03:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC