Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 23, 2018, 01:35:39 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Exercise 1  (Read 1818 times)
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;


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;


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 // = name; = email; = 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 = @"";   
        NSString  *aPhone = @"0231";
        NSString  *bName  = @"Stephen Teacher";   
        NSString  *bEmail = @"";
        NSString  *bPhone = @"0232";
        NSString  *cName  = @"Stephen Kochan";   
        NSString  *cEmail = @"";
        NSString  *cPhone = @"0233";
        NSString  *dName  = @"Jamie Baker";   
        NSString  *dEmail = @"";
        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.
Pages: [1]   Go Up
Jump to:  

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 ゥ 2009 All rights reserved.