Amazon.com Widgets Program 15.10
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 09:10:22 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Program Examples
| | |-+  Chapter 15
| | | |-+  Program 15.10
Pages: [1]   Go Down
Print
Author Topic: Program 15.10  (Read 1623 times)
TotalLuck
Full Member
***
Posts: 107



WWW Email




« on: February 15, 2009, 03:41:24 PM »

Interface file
Code: (Objective-C)

@interface AddressCard: NSObject
{
NSString *name;
NSString *email;
}
@property (copy, nonatomic) NSString *name, *email;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) print;
@end

@interface AddressBook : NSObject
{

NSString *bookName;
NSMutableArray *book;
}

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

@end
[/pre]

implementation
Code: (Objective-C)
@implementation AddressCard

@synthesize name, email;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
self.name = theName;
self.email = theEmail;
}

-(void) print
{
NSLog(@"====================================");
NSLog(@"| |");
NSLog(@"| %-31s |", [name UTF8String]);
NSLog(@"| %-31s |", [email UTF8String]);
NSLog(@"| |");
NSLog(@"| |");
NSLog(@"| |");
NSLog(@"| |");
NSLog(@"| O O |");
NSLog(@"====================================");

}


@end



@implementation AddressBook
// set up the Addressbook's name and 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: %@  =========", 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


[/pre]
Test Program

Code: (Objective-C)
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import "AddressBook.h"


int main(int argc, 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];

// set up Four addressCards

[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];

AddressBook *myBook = [ AddressBook alloc];

// Now initialize the Adress Book
myBook = [ myBook initWithName:@"L=inda'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


[myBook list];


[card1 release];
[card2 release];
[card3 release];
[card4 release];

[pool drain];

return 0;

}

[/pre]
« Last Edit: October 08, 2009, 01:02:38 PM by skochan » Logged

Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone http://bit.ly/h1fAJp
Shifty
Newbie
*
Posts: 8


Email




« Reply #1 on: August 08, 2009, 08:36:11 AM »

Code: (Objective-C)
@interface AddressBook : NSObject
{

NSString *bookName;
NSString *book;
}

NSString *book should be NSMutableArray *book.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.