Amazon.com Widgets Exercise 1
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 31, 2014, 02:25:46 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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 18
| | | |-+ Exercise 1
Pages: [1] Go Down
Print
Author Topic: Exercise 1 (Read 824 times)
Brabbeldas
Newbie
*
Posts: 10






on: January 26, 2012, 04:48:51 AM

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

#import "AddressCard.h"

@interface AddressBook : NSObject <NSCopying>

@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, strong) NSMutableArray *book;

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

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

-(int) entries;
-(void) list;
-(NSMutableArray *) lookup:(NSString *) theName;
-(bool) removeName: (NSString *) theName;
-(void) sort;

-(id) copyWithZone:(NSZone *)zone;

@end

The related method from the AddressBook implementation file:
Code: (Objective-C)
-(id) copyWithZone:(NSZone *)zone
{
    // Alloc and Init the new AddressBook object //
    AddressBook *newAddressBook = [[AddressBook allocWithZone:zone] init];
   
    // Set the bookName: the property has the 'copy' attribute, therefore simple assignment is sufficient //
    newAddressBook.bookName = bookName;
   
    // Make a deep copy of the book array: copy each individual card //
    for(AddressCard *card in book){
        // This line requires the AddressCard class to implement the <NSCopying> protocol //
        AddressCard *newCard = [card copy];
        [newAddressBook addCard: newCard];
    }
   
    return newAddressBook;
}


Interface file AddressCard.h
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface AddressCard : NSObject <NSCopying>

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

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

-(BOOL) isEqual:(AddressCard *) theCard;
-(NSComparisonResult) compareNames:(AddressCard *) theCard;

-(void) print;

-(id) copyWithZone:(NSZone *)zone;

@end

The related method from the AddressCard implementation file:
Code: (Objective-C)
-(id) copyWithZone:(NSZone *)zone
{
    AddressCard *newAddressCard = [[AddressCard allocWithZone:zone] init];
   
    // The properties of this class all have the copy attribute //
    // By using the setter methods a simple assignment should be sufficient //
    newAddressCard.name = name;
    newAddressCard.email = email;
    newAddressCard.phone = phone;
   
    return newAddressCard;
   
}


The main.m file for testing:
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "AddressBook.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
       
        // insert code here...
        NSString  *aName  = @"Julia Kochan";   
        NSString  *aEmail = @"jewls337@axlc.com";   
        NSString  *aPhone = @"0231";
        NSString  *bName  = @"Stephen Teacher";   
        NSString  *bEmail = @"tony.iannino@techfitness.com";
        NSString  *bPhone = @"0232";
        NSString  *cName  = @"Stephen Kochan";   
        NSString  *cEmail = @"steve@kochan-wood.com";
        NSString  *cPhone = @"0233";
        NSString  *dName  = @"Jamie Baker";   
        NSString  *dEmail = @"jbaker@kochan-wood.com";
        NSString  *dPhone = @"0234";
       
        AddressCard *card1 = [[AddressCard alloc] init];
        AddressCard *card2 = [[AddressCard alloc] init];
        AddressCard *card3 = [[AddressCard alloc] init];
        AddressCard *card4 = [[AddressCard alloc] init]; 
       
        // Create new AddressBook //
        AddressBook *myBook = [[AddressBook alloc] initWithName:@"My first AddressBook"];
       
        // Set properties of AddressCards //
        [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];   
       
        // Add cards to book //
        [myBook addCard:card1];
        [myBook addCard:card2];
        [myBook addCard:card3];
        [myBook addCard:card4];         
       
        // List all entries in book //
        [myBook list];
       
       
       
        AddressBook *myBook2 = [myBook copy];   
        myBook2.bookName = @"My second Book";   
             
        [myBook removeName: @"Jamie"];       
        [myBook list];   
        [myBook2 list]; 

    }
    return 0;
}


A) Would it make sense to also implement a mutableCopy method:
It would probably make sense. I can not think of a reason why not?

B) What would happen if someone uses the setter method for the book property in teh AddressBook class:
As the book property does not have the copy attribute only the reference would change. Therefore the AddressBook is no longer the owner.
Could be fixed by adding the copy property.


Could somebody please comment on my code? It would be much appreciated.
Thanks!
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.