Amazon.com Widgets January 12 Solutions
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 24, 2014, 07:46:02 PM
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
| |-+ Webcast Series Part II, Jan 5 - 28
| | |-+ January 12 Solutions
Pages: [1] Go Down
Print
Author Topic: January 12 Solutions (Read 2544 times)
daviddelmonte
Full Member
***
Posts: 123



WWW Email




on: January 14, 2010, 07:12:06 PM

Exercise 15-2... Some answers .. not all. I have added a new field (ex. 4), working on 2 and 5, and still to do 6. There is enough to improve here to invite comments/feedback/advice etc.

David

Address Card interface
Code: (Objective-C)
//
//  AddressCard.h
//  AddressCard
//
//  Created by David DelMonte on 1/14/10.
//  Copyright 2010 Bohemian Technologies. All rights reserved.
//
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>

@interface AddressCard: NSObject
{
    NSString *name;
    NSString *email;
NSString *phone;
}

// @property (copy, nonatomic) NSString *name, *email, *phone;

//-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;

-(void) setName: (NSString *) theName;
-(void) setEmail: (NSString *) theEmail;
-(void) setPhone: (NSString *) thePhone;

-(NSString *) name;
-(NSString *) email;
-(NSString *) phone;

-(void) print;

@end

Address Card Implementation
Code: (Objective-C)
//
//  AddressCard.h
//  AddressCard
//
//  Created by David DelMonte on 1/14/10.
//  Copyright 2010 Bohemian Technologies. All rights reserved.
//
#import "AddressCard.h"

@implementation AddressCard

//@synthesize name, email, phone;

-(void) setName:(NSString *) theName
{
if (name != theName)
{
[name release];
name = [[NSString alloc] initWithString: theName];
}
}

-(void) setEmail:(NSString *) theEmail
{
if (email != theEmail)
{
[email release];
email = [[NSString alloc] initWithString: theEmail];
}
}

-(void) setPhone:(NSString *) thePhone
{
if (phone != thePhone)
{
[phone release];
phone = [[NSString alloc] initWithString: thePhone];
}
}

-(NSString *) name
{
return name;
}

-(NSString *) email
{
return email;
}
-(NSString *) phone
{
return phone;
}


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

-(void) dealloc
{
[name release];
[email release];
[phone release];
[super dealloc];
}

@end

Address Book Interface
Code: (Objective-C)
//
//  AddressBook.h
//  AddressBook
//
//  Created by David DelMonte on 1/14/10.
//  Copyright 2010 Bohemian Technologies. All rights reserved.
//

#import <Foundation/NSArray.h>
#import "AddressCard.h"

@interface AddressBook: NSObject
{
    NSString       *bookName;
    NSMutableArray *book;
}

-(id) initWithName: (NSString *) name;

-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;

-(AddressCard *) lookup: (NSString *) theName;
-(int) entries;
-(void) list;

@end

Address Book Implementation
Code: (Objective-C)
//
//  AddressCard.h
//  AddressCard
//
//  Created by David DelMonte on 1/14/10.
//  Copyright 2010 Bohemian Technologies. All rights reserved.
//
#import "AddressBook.h"

@implementation AddressBook

// set up the AddressBook's name and a new book
-(id) initWithName: (NSString *) name
{
    self = [super init];
   
    if (self) {
        bookName = [[NSString alloc] initWithString: name];
        book     = [[NSMutableArray alloc] init];
    }
   
    return self;
}

// Add cards

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

// I have not yet implemented this next method!!

-(void) removeCard: (AddressCard *) theCard
{
    [book removeObjectIdenticalTo: theCard];
}

// lookup address card by name - assumes an exact match

-(AddressCard *) lookup: (NSString *) theName
{
    NSRange myRange;
    for ( AddressCard *nextCard in book ) {
        myRange = [[nextCard name] rangeOfString: theName
options: NSCaseInsensitiveSearch];
        if (myRange.location != NSNotFound)
            return nextCard;
    }

    return nil;
}


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

// Print out the AB list..

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

    NSLog(@"==================================================");
}

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

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

int cardCounter = 0;

int main (int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   
    //Create fields
NSString *aName  = @"David DelMonte";
    NSString *aEmail = @"ddelmonte@chinaGoogle.com";
NSString *aPhone = @"123.456.7890";
    NSString *bName  = @"Ian DelMonte";
    NSString *bEmail = @"ian@studiospark.com";
    NSString *bPhone = @"123.456.7890";
NSString *cName  = @"Stephen Kochan";
    NSString *cEmail = @"steve@kochan-wood.com";
NSString *cPhone = @"+1.212.555.1212";
    NSString *dName  = @"Beatrice Hamblett";
    NSString *dEmail = @"bea@famousArtist.com";
NSString *dPhone = @"+30 24240 24240";
   
//Create a search field

NSString *name = @"david";

// Set up four address cards

AddressCard *card1 = [[AddressCard alloc] init];
AddressCard *card2 = [[AddressCard alloc] init];   
AddressCard *card3 = [[AddressCard alloc] init];
AddressCard *card4 = [[AddressCard alloc] init];
   
    // Initialize the AB
AddressBook *myBook = [AddressBook alloc];
    AddressCard *myCard;
   
    /* I don't understand how to add a third field here
   
    [card1 setName: aName andEmail: aEmail];
    [card2 setName: bName andEmail: bEmail];
    [card3 setName: cName andEmail: cEmail];
    [card4 setName: dName andEmail: dEmail];

*/

// instead, add 3 fields to each card - boring!!

card1.name = aName; card1.email= aEmail; card1.phone = aPhone;
card2.name = bName; card2.email= bEmail; card2.phone = bPhone;
card3.name = cName; card3.email= cEmail; card3.phone = cPhone;
card4.name = dName; card4.email= dEmail; card4.phone = dPhone;
   
    myBook = [myBook initWithName: @"David's Address Book"];
   
    // Add the cards to my Address Book
   
    [myBook addCard: card1]; cardCounter++;
    [myBook addCard: card2]; cardCounter++;
    [myBook addCard: card3]; cardCounter++;
    [myBook addCard: card4]; cardCounter++;

    NSLog (@"There are %i cards in my Address Book\n", cardCounter);

    // Look up a person by name
   
    NSLog(@"Lookup: Lisa");
    myCard = [myBook lookup: @"Lisa"];
   
    if (myCard != nil)
        [myCard print];
    else
        NSLog(@"Not found!");

// Lookup another person, this time, through an NSString variable

NSLog(@"Lookup: %@", name);
    myCard = [myBook lookup: name];
   
    if (myCard != nil)
        [myCard print];
    else
        NSLog(@"Not found!");

    [myBook list];

//Manage Memory, keep fingers crossed, and hope Steve has a new little address card to add
// when he sees this :)
   
    [card1 release];
    [card2 release];
    [card3 release];
    [card4 release];
    [myBook release];
    [pool drain];
   
    return 0;
}

// I did not release "cardCounter" as I want to use instrments.app to see if there is a leak and what it looks like
Last Edit: January 14, 2010, 07:19:40 PM by daviddelmonte Logged
flyingmd
Newbie
*
Posts: 7


Email




Reply #1 on: January 15, 2010, 07:15:31 AM

Here is my attempt at some of the exercises. Still working on the last assignment

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

int main (int argc, const char * argv[]) { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    NSString    *aName = @"Jeff Sack"; //Steve, there has got to be an easier
    NSString    *aEmail = @"flyingmd@mac.com";  // way then typing all this crap in for
NSString *aPhone = @"123-456-7890"; // each card?????
    NSString    *aAddress1 = @"1234 Brian Court";
NSString    *aAddress2 = @"Suite 200"; 
    NSString    *aCity = @"Great Neck";
NSString *aState = @"NY";
NSString *aZip = @"11566";
    NSString    *bName = @"Brandon Sack";
    NSString    *bEmail = @"basack1@comcast.net"; 
    NSString    *cName = @"Lori Sack"; 
    NSString    *cEmail = @"lcsack@comcast.net"; 
    NSString    *dName = @"Jayme Sack"; 
    NSString    *dEmail = @"jaymesack@gmail.com"; 
    NSString    *eName = @"Alex Sack"; 
    NSString    *eEmail = @"deedles7@aol.com"; 
    AddressCard *card1 = [[AddressCard alloc] init]; 
    AddressCard *card2 = [[AddressCard alloc] init]; 
    AddressCard *card3 = [[AddressCard alloc] init]; 
    AddressCard *card4 = [[AddressCard alloc] init]; 
    AddressCard *card5 = [[AddressCard alloc] init]; 

    AddressBook *myBook = [AddressBook alloc]; 
    AddressCard *myCard; 
    NSArray     *myResult; 

    // First set up five address cards 

    [card1 setName: aName andEmail: aEmail];  // Cant figure out how to print several
[card1 setPhone: aPhone]; // fields on the same line????
[card1 setCity: aCity];
[card1 setState: aState];
[card1 setAddress1:aAddress1];
[card1 setAddress2: aAddress2];
[card1 setZip: aZip];
    [card2 setName: bName andEmail: bEmail]; 
    [card3 setName: cName andEmail: cEmail]; 
    [card4 setName: dName andEmail: dEmail]; 
    [card5 setName: eName andEmail: eEmail]; 

    myBook = [myBook initWithName: @"Jeff's Address Book"]; 

    // Add some cards to the address book 

    [myBook addCard: card1]; 
    [myBook addCard: card2]; 
    [myBook addCard: card3]; 
    [myBook addCard: card4]; 
    [myBook addCard: card5]; 

// The initial lookup routine

//NSLog(@"Lookup: Lori");
//myResult = [myBook lookup: @"Lori"];

//if (myResult != nil) { 
       // for (myCard in myResult) 
           // [myCard print];
//} 
    //else 
        //NSLog (@"Not found!"); 



    // Do a multiple look up of a person by last name 

    NSLog (@"LookupAll: Sack"); 
    myResult = [myBook lookupAll: @"Sack"]; 

    if (myResult != nil) { 
        for (myCard in myResult) 
            [myCard print]; 
    } 
    else 
        NSLog (@"Not found!"); 

    // another lookup example

    NSLog (@"LookupAll: Jeff"); 
    myResult = [myBook lookupAll: @"jeff"]; 

    if (myResult != nil) { 
        for (myCard in myResult) 
            [myCard print]; 
    } 
    else 
        NSLog (@"Not found!"); 

    //  another lookup example

    NSLog (@"LookupAll: Alex"); 
    myResult = [myBook lookupAll: @"alex"]; 

    if (myResult != nil) { 
        for (myCard in myResult) 
            [myCard print]; 
    } 
    else 
        NSLog (@"Not found!"); 


    [card1 release];  //release the objects we initialized
    [card2 release]; 
    [card3 release]; 
    [card4 release]; 
    [card5 release]; 
    [myBook release]; 

    [pool drain];  // autorelease pool drained
    return 0; 

Code: (Objective-C)
//
//  AddressBook.h
//  AddressBook
//
//  Created by Jeffrey Sack on 1/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AddressCard.h" 

@interface AddressBook : NSObject 

    NSString        *bookName; 
    NSMutableArray  *book; 

-(id)               initWithName: (NSString *) name; 
-(void)             addCard: (AddressCard *) theCard; 
-(int)              entries; 
-(void)             list; 
-(void)             dealloc; 
-(AddressCard *)    lookup: (NSString *) theName; 
-(NSArray *)        lookupAll: (NSString *) theName; 
@end

Code: (Objective-C)
//  AddressBook.m
//  AddressBook
//
//  Created by Jeffrey Sack on 1/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//


#import "AddressBook.h" 


@implementation AddressBook 

// here we 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 (@"===================================================="); 


-(AddressCard *) lookup: (NSString *) theName 

    NSRange textSearch; 
    for ( AddressCard *nextCard in book ) { 
        textSearch = [[nextCard name] rangeOfString: theName   
                                            options: NSCaseInsensitiveSearch]; 
        if ( textSearch.location != NSNotFound ) 
            return nextCard; 
    } 

    return nil; 


-(NSArray *) lookupAll: (NSString *) theName 

    NSRange textSearch; 
    NSMutableArray *result = [NSMutableArray array]; 
    BOOL hasResult = NO; 
    for ( AddressCard *nextCard in book ) { 
        textSearch = [[nextCard name] rangeOfString: theName   
                                            options: NSCaseInsensitiveSearch]; 
        if ( textSearch.location != NSNotFound ){  // we found a match
            [result addObject: nextCard]; 
            hasResult = YES; 
        } 
    } 

    if ( hasResult ) 
        return result; 
    else 
        return nil; 


-(void) dealloc 

    [bookName release]; 
    [book release]; 
    [super dealloc]; 

@end 


 

Code: (Objective-C)
//
//  AddressCard.h
//  AddressBook
//
//  Created by Jeffrey Sack on 1/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>  // these are redundant?
#import <Foundation/NSString.h>  // these are redundant?

@interface AddressCard : NSObject { 
    NSString *name; 
    NSString *email;
NSString *phone;
NSString *address1;
NSString *address2;
NSString *city;
NSString *state;
NSString *zip;

@property(copy, nonatomic) NSString *name, *email, *phone, *address1, *address2, *city, *state, *zip; 
-(void) print; 
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail; 
@end 

Code: (Objective-C)
//
//  AddressCard.m
//  AddressBook
//
//  Created by Jeffrey Sack on 1/13/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AddressCard.h" 


@implementation AddressCard 
@synthesize name, email, phone, address1, address2, city, state, zip; 

-(void) print 

    NSLog (@"====================================="); 
    NSLog (@"|                                   |"); 
    NSLog (@"|  %-31s  |", [name UTF8String]); 
    NSLog (@"|  %-31s  |", [email UTF8String]); 
    NSLog (@"|  %-31s  |", [phone UTF8String]);                                 
    NSLog (@"|  %-31s  |", [address1 UTF8String]);                               
    NSLog (@"|  %-31s  |", [address2 UTF8String]);
NSLog (@"|  %-31s  |", [city UTF8String]);
NSLog (@"|  %-31s  |", [state UTF8String]);
NSLog (@"|  %-31s  |", [zip UTF8String]);
    NSLog (@"|      O                    O       |"); 
    NSLog (@"====================================="); 



-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail 

    self.name = theName; 
    self.email = theEmail; 

@end 
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email




Reply #2 on: January 15, 2010, 07:31:38 AM

Looks great Jeff. One thing right away.. If you want to display the third field, you need to change this section:

Code: (Objective-C)
    NSLog (@"======== Contents of : %@ ========", bookName);    
      
    for ( AddressCard *theCard in book )    
        NSLog (@"%-20s    %-32s", [theCard.name UTF8String],    
               [theCard.email UTF8String]);    
      
    NSLog (@"====================================================");    
}

to something like this:

Code: (Objective-C)
 NSLog(@"%-20s  %-32s  %-50s", [theCard.name UTF8String],  

David
Logged
flyingmd
Newbie
*
Posts: 7


Email




Reply #3 on: January 15, 2010, 04:23:24 PM

David, Thanks for the input. Im pretty new to all of this. Still trying to modify. Would like an easier way to NSLog all the fields without the repetitive lines of code....... ie. just call the print method to the object and have the object format all the fields for printing to the rolodex card.

Jeff
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email




Reply #4 on: January 16, 2010, 02:12:45 PM

Ok. So I implemented the removeCard method, and printed the AB twice to test it.. Here's the code I changed. Onwards...

AddressCard.h

Code: (Objective-C)
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andPhone: (NSString *)thePhone;

//-(void) setName: (NSString *) theName;
//-(void) setEmail: (NSString *) theEmail;
//-(void) setPhone: (NSString *) thePhone;

AddressCard.m
Code: (Objective-C)
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andPhone: (NSString *)thePhone

{
if (name != theName)
{
[name release];
name = [[NSString alloc] initWithString: theName];
}



if (email != theEmail)
{
[email release];
email = [[NSString alloc] initWithString: theEmail];
}



if (phone != thePhone)
{
[phone release];
phone = [[NSString alloc] initWithString: thePhone];
}


AddressBook.h

Code: (Objective-C)
// Remove card method

-(void) removeCard: (AddressCard *) theCard
{
    [book removeObjectIdenticalTo: theCard];
}

AddressBook ab.m

Code: (Objective-C)
[card1 setName: aName andEmail: aEmail andPhone: aPhone];
[card2 setName: bName andEmail: bEmail andPhone: bPhone];
[card3 setName: cName andEmail: cEmail andPhone: cPhone];
[card4 setName: dName andEmail: dEmail andPhone: dPhone];

/* At first, I added 3 fields to each card - boring!!

card1.name = aName; card1.email= aEmail; card1.phone = aPhone;
card2.name = bName; card2.email= bEmail; card2.phone = bPhone;
card3.name = cName; card3.email= cEmail; card3.phone = cPhone;
card4.name = dName; card4.email= dEmail; card4.phone = dPhone;
    */

Code: (Objective-C)
//Use the same variable to remove that card

NSLog(@"Lookup: %@", name);
    myCard = [myBook lookup: name];
    
    if (myCard != nil)
{
        [myCard print];
[myBook removeCard: myCard];
NSLog(@"The following card was removed: %@\n", name);
}
    else
        NSLog(@"Not found!\n");

//print the revised AB

[myBook list];

With some formatting, the output looks like this..

Last Edit: January 16, 2010, 02:23:58 PM by daviddelmonte Logged
flyingmd
Newbie
*
Posts: 7


Email




Reply #5 on: January 16, 2010, 06:00:39 PM

David,

On your removeCard method, why use removeObjectIdenticalTo and not just removeObject method? I can't see how it would make a difference in this example. Why did you choose that one?

Jeff
Logged
daviddelmonte
Full Member
***
Posts: 123



WWW Email




Reply #6 on: January 16, 2010, 06:30:06 PM

Hi Jeff, I used it because I saw it in an example some where Smiley

However, I think that it checks that the card I'm selecting meets the criteria. But I will try the removeObject method and see what happens. One of the things I like about these languages (compared to the old days), is that you get immediate feedback - yes that worked - or no that didn't.

David
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.