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

ELBdelorean

macrumors member
Original poster
May 18, 2010
98
0
Hi all,

I have been reading Programming in Objective-C 2.0 by Stephen Kochan and one of the programs in chapter 15 is giving me a weird output.

Whenever I run Program 15.10, this is the output I get:

Code:
Entries in address book after creation: 0
Entries in address book after adding cards: 4
======== Contents of 0x1.fffd7effd44p-1028 ========
Julia Kochan* * * * * * * jewls337@axlc.com* * * * * * * *
Tony Iannino* * * * * * * tony.iannino@techfitness.com* * 
Stephen Kochan* * * * * * steve@kochan-wood.com* * * * * *
Jamie Baker* * * * * * * *jbaker@kochan-wood.com* * * * * 
===================================================

Here's what I have:

AddressBook.h Interface file:
Code:
#import "AddressCard.h"
#import <Foundation/NSArray.h>


@interface AddressBook : NSObject {

	NSString *bookName;
	NSMutableArray *book;
}

-(id) initWithName: (NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(void) dealloc;

@end

AddressBook.m Implementation file:
Code:
#import "AddressBook.h"


@implementation AddressBook

//set up the AddressBook's name as an empty book

-(id) initWithName:(NSString *)name
{
	self = [super init];
	
	if (self) {
		bookName = [[NSString alloc] initWithString: name];
		book = [[NSMutableArray alloc] init];
	}
	return self;
}

-(void) addCard:(AddressCard *)theCard
{
	[book addObject: theCard];
}

-(int) entries
{
	return [book count];
}

-(void) list
{
	NSLog(@"======== Contents of %a ========", bookName);
	
	for (AddressCard *theCard in book)
		NSLog(@"%-20s* * * %-32s", [theCard.name UTF8String],
			[theCard.email UTF8String]);
	NSLog(@"===================================================");
			
}

-(void) dealloc
{
	[bookName release];
	[book release];
	[super dealloc];
}
@end

Test file:
Code:
#import "AddressBook.h"
#import <Foundation/NSAutoreleasePool.h>

int main (int argc, const char * argv[]) {
* * NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString *aName = @"Julia Kochan";
	NSString *aEmail = @"jewls337@axlc.com";
	NSString *bName = @"Tony Iannino";
	NSString *bEmail = @"tony.iannino@techfitness.com";
	NSString *cName = @"Stephen Kochan";
	NSString *cEmail = @"steve@kochan-wood.com";
	NSString *dName = @"Jamie Baker";
	NSString *dEmail = @"jbaker@kochan-wood.com";
	
	AddressCard *card1 = [[AddressCard alloc] init];
	AddressCard *card2 = [[AddressCard alloc] init];
	AddressCard *card3 = [[AddressCard alloc] init];
	AddressCard *card4 = [[AddressCard alloc] init];
	
	AddressBook *myBook = [AddressBook alloc];
	
	//First set up four address cards
	
	[card1 setName: aName andEmail: aEmail];
	[card2 setName: bName andEmail: bEmail];
	[card3 setName: cName andEmail: cEmail];
	[card4 setName: dName andEmail: dEmail];
	
	//Now initialize the address book
	
	myBook = [myBook initWithName:@"Linda's Address Book"];
	
	NSLog(@"Entries in address book after creation: %i", [myBook entries]);
	
	//Add some cards to the address book
	
	[myBook addCard: card1];
	[myBook addCard: card2];
	[myBook addCard: card3];
	[myBook addCard: card4];
	
	NSLog(@"Entries in address book after adding cards: %i", [myBook entries]);
	
	//List all the entries in the book now
	
	[myBook list];
		
	[card1 release];* * 
* * [card2 release];
	[card3 release];
	[card4 release];
	[myBook release];
	
	[pool drain];
* * return 0;
}
 

McGordon

macrumors member
Dec 28, 2010
63
1
Scotland
You had the %a conversion instead of %@ to display the object (NSString).

Code:
-(void) list
{
	NSLog(@"======== Contents of [COLOR="Green"]%@[/COLOR] ========", bookName);
	
	for (AddressCard *theCard in book)
		NSLog(@"%-20s* * * %-32s", [theCard.name UTF8String],
			[theCard.email UTF8String]);
	NSLog(@"===================================================");
			
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.