Amazon.com Widgets Chapter 15 Exercise 5
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 03:52:47 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
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 15
| | | |-+  Chapter 15 Exercise 5
Pages: [1]   Go Down
Print
Author Topic: Chapter 15 Exercise 5  (Read 526 times)
clouded
Full Member
***
Posts: 123






« on: June 13, 2012, 07:35:58 PM »

Spent too much time thinking about this:
Quote
Can you think of a way to design your AddressCard and AddressBook classes so that the latter does not have to know all the fields stored in the former?
first half of the exercise here... the answer to this question is below.
 
Here's what I did on this exercise:

Code: (Objective-C)
//  Chapter 15 Exercise 5
// After completing exercise 4, modify the lookup: method from exercise 3 to perform
// a search on all the fields of an address card. Can you think of a way to design
// your AddressCard and AddressBook classes so that the latter does not have to know
// all the fields stored in the former?

//  main.m

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

int main (int argc, char * argv[])
{
    @autoreleasepool {
        
        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@classroomM.com";
        NSString *dName = @"Jamie Baker";
        NSString *dEmail = @"jbaker@classroomM.com";
        NSString *eName = @"Ella Smith";
        NSString *eEmail = @"ella.smith@csmithent.com";
        NSString *eAddress = @"123 Main Street";
        NSString *eCity = @"Anywhere";
        NSString *eZip = @"12345";
        NSString *eCountry = @"USA";
        NSString *ePhone = @"(212) 555-1212";
        
        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] initWithName: @"Linda’s Address Book"];
        AddressBook *myResults;
        
        // 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];
        [card5 setName: eName andEmail: eEmail andAddress: eAddress
               andCity: eCity andZip: eZip andCountry: eCountry andPhone: ePhone];
        
        // Add some cards to the address book
        [myBook addCard: card1];
        [myBook addCard: card2];
        [myBook addCard: card3];
        [myBook addCard: card4];
        [myBook addCard: card5];
        
        // List the unsorted book
        [myBook list];
        
        // Look up a person by name
        NSLog (@"lookup: steve");
        myResults = [myBook lookup: @"steve"];
        
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
        
        // Try another lookup
        NSLog (@"lookup: koc");
        myResults = [myBook lookup: @"koc"];
        
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
        
        // Last one
        NSLog (@"lookup: nak");
        myResults = [myBook lookup: @"nak"];
        
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
        
        // Last one... this time for real!!
        NSLog (@"lookup: 1212");
        myResults = [myBook lookup: @"1212"];
        
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
    }
    return 0;
}
Code: (Objective-C)
//  AddressBook.h

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

@interface AddressBook: NSObject

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

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

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

-(AddressBook *) lookup: (NSString *) theName;
-(int) entries;
-(void) list;
-(void) sort;
-(void) printCards;

@end
Code: (Objective-C)
//  AddressBook.m

#import "AddressBook.h"

@implementation AddressBook
@synthesize bookName, book;
-(id) initWithName: (NSString *) name
{
    self = [super init];
    
    if (self) {
        bookName = [NSString stringWithString: name];
        book = [NSMutableArray array];
    }
    return self;
}
-(id) init
{
    return [self initWithName: @"NoName"];
}
-(void) addCard: (AddressCard *) theCard
{
    [book addObject: theCard];
}
-(void) removeCard: (AddressCard *) theCard
{
    [book removeObjectIdenticalTo: theCard];
}
-(AddressBook *) lookup: (NSString *) theName
{
    AddressBook *results = [[AddressBook alloc] initWithName: @"lookup results"];
    
    for ( AddressCard *nextCard in book ) {
        if ( [nextCard.name rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.address rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.city rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.zip rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.country rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.phone rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.email rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
    }
    
    if (results.book.count == 0)
        return nil;
    
    return results;
}
-(int) entries
{
    return (int)[book count];
}
-(void) list
{
    NSLog (@"======== Contents of: %@ =========", bookName);
    
    for ( AddressCard *theCard in book )
        NSLog (@"%-20s   %-32s", [theCard.name UTF8String], [theCard.email UTF8String]);
    
    NSLog (@"==================================================");
}
-(void) sort
{
    [book sortUsingSelector: @selector(compareNames:)];
}
-(void) printCards
{
    NSLog (@"======== Contents of: %@ =========", bookName);
    
    for ( AddressCard *theCard in book ) {
        NSLog (@"====================================");
        NSLog (@"|                                  |");
        NSLog (@"|  %-31s |", [theCard.name UTF8String]);
        NSLog (@"|  %-31s |", [theCard.firstname UTF8String]);
        NSLog (@"|  %-31s |", [theCard.lastname UTF8String]);
        NSLog (@"|  %-31s |", [theCard.address UTF8String]);
        NSLog (@"|  %-31s |", [theCard.city UTF8String]);
        NSLog (@"|  %-31s |", [theCard.zip UTF8String]);
        NSLog (@"|  %-31s |", [theCard.country UTF8String]);
        NSLog (@"|  %-31s |", [theCard.phone UTF8String]);
        NSLog (@"|  %-31s |", [theCard.email UTF8String]);
        NSLog (@"|                                  |");
        NSLog (@"|                                  |");
        NSLog (@"|                                  |");
        NSLog (@"====================================");
        NSLog (@" ");
    }
}
@end
Code: (Objective-C)
//  AddressCard.h

#import <Foundation/Foundation.h>

@interface AddressCard: NSObject

@property (copy, nonatomic) NSString *name, *firstname, *lastname, *address, *city, *zip, *country, *phone, *email;

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAddress: (NSString *) theAddress andCity: (NSString *) theCity
         andZip: (NSString *) theZip andCountry: (NSString *) theCountry andPhone: (NSString *) thePhone;
-(void) print;
-(void) printAll;

-(NSComparisonResult) compareNames: (id) element;

@end
Code: (Objective-C)
//  AddressCard.m

#import "AddressCard.h"

@implementation AddressCard
@synthesize name, firstname, lastname, address, city, zip, country, phone, email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
    self.name = theName;
    NSRange space = [[self name] rangeOfString: @" "];
    self.firstname = [[self name] substringToIndex: space.location];
    self.lastname = [[self name] substringFromIndex: space.location + 1];
    self.email = theEmail;
    self.address = @" ";
    self.city = @" ";
    self.zip = @" ";
    self.country = @" ";
    self.phone = @" ";
}
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAddress: (NSString *) theAddress andCity: (NSString *) theCity
         andZip: (NSString *) theZip andCountry: (NSString *) theCountry andPhone: (NSString *) thePhone
{
    self.name = theName;
    NSRange space = [[self name] rangeOfString: @" "];
    self.email = theEmail;
    self.firstname = [[self name] substringToIndex: space.location];
    self.lastname = [[self name] substringFromIndex: space.location + 1];
    self.address = theAddress;
    self.city = theCity;
    self.zip = theZip;
    self.country = theCountry;
    self.phone = thePhone;
}
-(void) print
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"====================================");
}
-(void) printAll
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [firstname UTF8String]);
    NSLog (@"|  %-31s |", [lastname UTF8String]);
    NSLog (@"|  %-31s |", [address UTF8String]);
    NSLog (@"|  %-31s |", [city UTF8String]);
    NSLog (@"|  %-31s |", [zip UTF8String]);
    NSLog (@"|  %-31s |", [country UTF8String]);
    NSLog (@"|  %-31s |", [phone UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"====================================");
}
-(NSComparisonResult) compareNames: (id) element
{
    return [name compare: [element name]];
}
@end

Output:

======== Contents of: Linda’s Address Book =========
Julia Kochan           jewls337@axlc.com              
Tony Iannino           tony.iannino@techfitness.com    
Stephen Kochan         steve@classroomM.com            
Jamie Baker            jbaker@classroomM.com          
Ella Smith             ella.smith@csmithent.com        
==================================================
lookup: steve
======== Contents of: lookup results =========
====================================
|                                  |
|  Stephen Kochan                  |
|  Stephen                         |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  steve@classroomM.com            |
|                                  |
|                                  |
|                                  |
====================================
  
lookup: koc
======== Contents of: lookup results =========
====================================
|                                  |
|  Julia Kochan                    |
|  Julia                           |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  jewls337@axlc.com               |
|                                  |
|                                  |
|                                  |
====================================

====================================
|                                  |
|  Stephen Kochan                  |
|  Stephen                         |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  steve@classroomM.com            |
|                                  |
|                                  |
|                                  |
====================================

lookup: nak
Not found!
lookup: 1212
======== Contents of: lookup results =========
====================================
|                                  |
|  Ella Smith                      |
|  Ella                            |
|  Smith                           |
|  123 Main Street                 |
|  Anywhere                        |
|  12345                           |
|  USA                             |
|  (212) 555-1212                  |
|  ella.smith@csmithent.com        |
|                                  |
|                                  |
|                                  |
====================================
« Last Edit: June 14, 2012, 07:56:35 AM by clouded » Logged
clouded
Full Member
***
Posts: 123






« Reply #1 on: June 14, 2012, 07:50:56 AM »

Here's my answer to the second question
Quote
Can you think of a way to design your AddressCard and AddressBook classes so that the latter does not have to know all the fields stored in the former?

Code: (Objective-C)
//  Chapter 15 Exercise 5
// After completing exercise 4, modify the lookup: method from exercise 3 to perform
// a search on all the fields of an address card. Can you think of a way to design
// your AddressCard and AddressBook classes so that the latter does not have to know
// all the fields stored in the former?

//  main.m

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

int main (int argc, char * argv[])
{
    @autoreleasepool {
       
        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@classroomM.com";
        NSString *dName = @"Jamie Baker";
        NSString *dEmail = @"jbaker@classroomM.com";
        NSString *eName = @"Ella Smith";
        NSString *eEmail = @"ella.smith@csmithent.com";
        NSString *eAddress = @"123 Main Street";
        NSString *eCity = @"Anywhere";
        NSString *eZip = @"12345";
        NSString *eCountry = @"USA";
        NSString *ePhone = @"(212) 555-1212";
       
        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] initWithName: @"Linda’s Address Book"];
        AddressBook *myResults;
       
        // 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];
        [card5 setName: eName andEmail: eEmail andAddress: eAddress
               andCity: eCity andZip: eZip andCountry: eCountry andPhone: ePhone];
       
        // Add some cards to the address book
        [myBook addCard: card1];
        [myBook addCard: card2];
        [myBook addCard: card3];
        [myBook addCard: card4];
        [myBook addCard: card5];
       
        // List the unsorted book
        [myBook list];
       
        // Look up a person by name
        NSLog (@"lookup: steve");
        myResults = [myBook lookupWithoutKnowingFields: @"steve"];
       
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
       
        // Try another lookup
        NSLog (@"lookup: koc");
        myResults = [myBook lookupWithoutKnowingFields: @"koc"];
       
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
       
        // Last one
        NSLog (@"lookup: nak");
        myResults = [myBook lookupWithoutKnowingFields: @"nak"];
       
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
       
        // Last one... this time for real!!
        NSLog (@"lookup: 1212");
        myResults = [myBook lookupWithoutKnowingFields: @"1212"];
       
        if (myResults != nil)
            [myResults printCards];
        else
            NSLog (@"Not found!");
    }
    return 0;
}
Code: (Objective-C)
//  AddressBook.h

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

@interface AddressBook: NSObject

@property (nonatomic, copy) NSString *bookName;
@property (nonatomic, strong) NSMutableArray *book;
-(id) initWithName: (NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;
-(AddressBook *) lookup: (NSString *) theName;
-(AddressBook *) lookupWithoutKnowingFields: (NSString *) theName;
-(int) entries;
-(void) list;
-(void) sort;
-(void) printCards;

@end
Code: (Objective-C)
//  AddressBook.m

#import "AddressBook.h"

@implementation AddressBook
@synthesize bookName, book;
-(id) initWithName: (NSString *) name
{
    self = [super init];
   
    if (self) {
        bookName = [NSString stringWithString: name];
        book = [NSMutableArray array];
    }
    return self;
}
-(id) init
{
    return [self initWithName: @"NoName"];
}
-(void) addCard: (AddressCard *) theCard
{
    [book addObject: theCard];
}
-(void) removeCard: (AddressCard *) theCard
{
    [book removeObjectIdenticalTo: theCard];
}
-(AddressBook *) lookup: (NSString *) theName
{
    AddressBook *results = [[AddressBook alloc] initWithName: @"lookup results"];
   
    for ( AddressCard *nextCard in book ) {
        if ( [nextCard.name rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.address rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.city rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.zip rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.country rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.phone rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
        else if ( [nextCard.email rangeOfString: theName options: NSCaseInsensitiveSearch].location != NSNotFound)
            [results addCard: nextCard];
    }
   
    if (results.book.count == 0)
        return nil;
   
    return results;
}
-(AddressBook *) lookupWithoutKnowingFields: (NSString *) theName
{
    AddressBook *results = [[AddressBook alloc] initWithName: @"lookup results"];
   
    for ( AddressCard *nextCard in book ) {
        if ( [nextCard search: theName])
            [results addCard: nextCard];
    }
   
    if (results.book.count == 0)
        return nil;
   
    return results;
}
-(int) entries
{
    return (int)[book count];
}
-(void) list
{
    NSLog (@"======== Contents of: %@ =========", bookName);
   
    for ( AddressCard *theCard in book )
        NSLog (@"%-20s   %-32s", [theCard.name UTF8String], [theCard.email UTF8String]);
   
    NSLog (@"==================================================");
}
-(void) sort
{
    [book sortUsingSelector: @selector(compareNames:)];
}
-(void) printCards
{
    NSLog (@"======== Contents of: %@ =========", bookName);
   
    for ( AddressCard *theCard in book ) {
        NSLog (@"====================================");
        NSLog (@"|                                  |");
        NSLog (@"|  %-31s |", [theCard.name UTF8String]);
        NSLog (@"|  %-31s |", [theCard.firstname UTF8String]);
        NSLog (@"|  %-31s |", [theCard.lastname UTF8String]);
        NSLog (@"|  %-31s |", [theCard.address UTF8String]);
        NSLog (@"|  %-31s |", [theCard.city UTF8String]);
        NSLog (@"|  %-31s |", [theCard.zip UTF8String]);
        NSLog (@"|  %-31s |", [theCard.country UTF8String]);
        NSLog (@"|  %-31s |", [theCard.phone UTF8String]);
        NSLog (@"|  %-31s |", [theCard.email UTF8String]);
        NSLog (@"|                                  |");
        NSLog (@"|                                  |");
        NSLog (@"|                                  |");
        NSLog (@"====================================");
        NSLog (@" ");
    }
}
@end
Code: (Objective-C)
//  AddressCard.h

#import <Foundation/Foundation.h>

@interface AddressCard: NSObject

@property (copy, nonatomic) NSString *name, *firstname, *lastname, *address, *city, *zip, *country, *phone, *email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAddress: (NSString *) theAddress andCity: (NSString *) theCity
         andZip: (NSString *) theZip andCountry: (NSString *) theCountry andPhone: (NSString *) thePhone;
-(void) print;
-(void) printAll;
-(BOOL) search: (NSString *) criteria;
-(NSComparisonResult) compareNames: (id) element;

@end
Code: (Objective-C)
//  AddressCard.m

#import "AddressCard.h"

@implementation AddressCard
@synthesize name, firstname, lastname, address, city, zip, country, phone, email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
    self.name = theName;
    NSRange space = [[self name] rangeOfString: @" "];
    self.firstname = [[self name] substringToIndex: space.location];
    self.lastname = [[self name] substringFromIndex: space.location + 1];
    self.email = theEmail;
    self.address = @" ";
    self.city = @" ";
    self.zip = @" ";
    self.country = @" ";
    self.phone = @" ";
}
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail andAddress: (NSString *) theAddress andCity: (NSString *) theCity
         andZip: (NSString *) theZip andCountry: (NSString *) theCountry andPhone: (NSString *) thePhone
{
    self.name = theName;
    NSRange space = [[self name] rangeOfString: @" "];
    self.email = theEmail;
    self.firstname = [[self name] substringToIndex: space.location];
    self.lastname = [[self name] substringFromIndex: space.location + 1];
    self.address = theAddress;
    self.city = theCity;
    self.zip = theZip;
    self.country = theCountry;
    self.phone = thePhone;
}
-(void) print
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"====================================");
}
-(void) printAll
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [firstname UTF8String]);
    NSLog (@"|  %-31s |", [lastname UTF8String]);
    NSLog (@"|  %-31s |", [address UTF8String]);
    NSLog (@"|  %-31s |", [city UTF8String]);
    NSLog (@"|  %-31s |", [zip UTF8String]);
    NSLog (@"|  %-31s |", [country UTF8String]);
    NSLog (@"|  %-31s |", [phone UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"====================================");
}
-(BOOL) search: (NSString *) criteria
{
    if ( [self.name rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.address rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.city rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.zip rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.country rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.phone rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
    else if ( [self.email rangeOfString: criteria options: NSCaseInsensitiveSearch].location != NSNotFound)
        return YES;
   
    return NO;
}
-(NSComparisonResult) compareNames: (id) element
{
    return [name compare: [element name]];
}
@end

Output:

======== Contents of: Linda’s Address Book =========
Julia Kochan           jewls337@axlc.com               
Tony Iannino           tony.iannino@techfitness.com   
Stephen Kochan         steve@classroomM.com           
Jamie Baker            jbaker@classroomM.com           
Ella Smith             ella.smith@csmithent.com       
==================================================
lookup: steve
======== Contents of: lookup results =========
====================================
|                                  |
|  Stephen Kochan                  |
|  Stephen                         |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  steve@classroomM.com            |
|                                  |
|                                  |
|                                  |
====================================
 
lookup: koc
======== Contents of: lookup results =========
====================================
|                                  |
|  Julia Kochan                    |
|  Julia                           |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  jewls337@axlc.com               |
|                                  |
|                                  |
|                                  |
====================================

====================================
|                                  |
|  Stephen Kochan                  |
|  Stephen                         |
|  Kochan                          |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|  steve@classroomM.com            |
|                                  |
|                                  |
|                                  |
====================================

lookup: nak
Not found!
lookup: 1212
======== Contents of: lookup results =========
====================================
|                                  |
|  Ella Smith                      |
|  Ella                            |
|  Smith                           |
|  123 Main Street                 |
|  Anywhere                        |
|  12345                           |
|  USA                             |
|  (212) 555-1212                  |
|  ella.smith@csmithent.com        |
|                                  |
|                                  |
|                                  |
====================================
Logged
mo7ionsickness
Jr. Member
**
Posts: 50






« Reply #2 on: September 11, 2012, 09:57:55 AM »

Code: (Objective-C)
-(NSMutableArray *) lookupAll:(NSString *)theName
{
    NSMutableArray *matches = [NSMutableArray array];
   
    [book indexesOfObjectsPassingTest:
      ^ (id obj, NSUInteger idx, BOOL *stop)
        {
            NSRange subRange;
           
            subRange = [[obj name] rangeOfString:theName options:NSCaseInsensitiveSearch];
           
            if (subRange.location != NSNotFound) {
                [matches addObject:obj];
                return YES;
            }
            else
                return NO;
        }];

    [book indexesOfObjectsPassingTest:
     ^ (id obj, NSUInteger idx, BOOL *stop)
     {
         NSRange subRange;
         
         subRange = [[obj email] rangeOfString:theName options:NSCaseInsensitiveSearch];
         
         if (subRange.location != NSNotFound) {
             [matches addObject:obj];
             return YES;
         }
         else
             return NO;
     }];
   
    [book indexesOfObjectsPassingTest:
     ^ (id obj, NSUInteger idx, BOOL *stop)
     {
         NSRange subRange;
         
         subRange = [[obj addresss] rangeOfString:theName options:NSCaseInsensitiveSearch];
         
         if (subRange.location != NSNotFound) {
             [matches addObject:obj];
             return YES;
         }
         else
             return NO;
     }];
   
    if ([matches count])
        return matches;
    else
        return nil;
   
}
Code: (Objective-C)
        NSString *aName    = @"Julia Kochan";
        NSString *aEmail   = @"jewls337@axlc.com";
        NSString *aAddress = @"USA";
        NSString *bName    = @"Tony Iannino";
        NSString *bEmail   = @"tony.iannino@techfitness.com";
        NSString *bAddress = @"Italy";
        NSString *cName    = @"Stephen Kochan";
        NSString *cEmail   = @"steve@classroom.com";
        NSString *cAddress = @"USA";
        NSString *dName    = @"Jamie Baker";
        NSString *dEmail   = @"jbaker@classroom.com";
        NSString *dAddress = @"UK";
       
        AddressCard *card1  = [[AddressCard alloc] init];
        AddressCard *card2  = [[AddressCard alloc] init];
        AddressCard *card3  = [[AddressCard alloc] init];
        AddressCard *card4  = [[AddressCard alloc] init];
       
        AddressBook *myBook = [[AddressBook alloc]initWithName:@"Linda's AddressBook"];
       
        [card1 setName:aName andEmail:aEmail andAddress:aAddress];
        [card2 setName:bName andEmail:bEmail andAddress:bAddress];
        [card3 setName:cName andEmail:cEmail andAddress:cAddress];
        [card4 setName:dName andEmail:dEmail andAddress:dAddress];
       
        [myBook addCard:card1];
        [myBook addCard:card2];
        [myBook addCard:card3];
        [myBook addCard:card4];
       
        for (AddressCard *theCard in [myBook lookupAll:@"classroom"])
            NSLog(@"%-14s    %-32s %-40s", [theCard.name UTF8String],
                  [theCard.email UTF8String], [theCard.addresss UTF8String]);
2012-09-11 17:56:37.688 Address Book[495:303] Stephen Kochan    steve@classroom.com              USA                                     
2012-09-11 17:56:37.690 Address Book[495:303] Jamie Baker       jbaker@classroom.com             UK     
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.