PDA

View Full Version : "Contents of 0x1.fffd7effd44p-1028" ?




ELBdelorean
Apr 29, 2011, 01:45 PM
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:

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:
#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:
#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:
#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;
}



lee1210
Apr 29, 2011, 02:08 PM
%@ to print objects, %a to print a memory address.

-Lee

Oops. %a is:
64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent.
From:
http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Looked addressy, but I thought %p was the only thing for that.

McGordon
Apr 29, 2011, 02:17 PM
You had the %a conversion instead of %@ to display the object (NSString).


-(void) list
{
NSLog(@"======== Contents of %@ ========", bookName);

for (AddressCard *theCard in book)
NSLog(@"%-20s* * * %-32s", [theCard.name UTF8String],
[theCard.email UTF8String]);
NSLog(@"===================================================");

}

ELBdelorean
Apr 29, 2011, 02:21 PM
Of course! I feel so stupid now. Thanks guys.