Amazon.com Widgets 15-3 Simple way
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 01, 2014, 11:30:45 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
| |-+  Answers to Exercises
| | |-+  Chapter 15
| | | |-+  15-3 Simple way
Pages: [1]   Go Down
Print
Author Topic: 15-3 Simple way  (Read 1151 times)
tadej5553
Full Member
***
Posts: 145


Email




« on: May 21, 2010, 12:51:15 PM »

size=13pt]AddressCard.h[/size]
Code: (Objective-C)
#import <Foundation/Foundation.h> 

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

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

-(AddressCard *) setName: (NSString *) n andEmail: (NSString *) e;
-(void) print;
@end

AddressCard.m
Code: (Objective-C)
#import "AddressCard.h" 

@implementation AddressCard

@synthesize name, email;
-(void) print
{
    NSLog (@"====================================");
    NSLog (@"|                                  |");
    NSLog (@"|  %-31s |", [name UTF8String]);
    NSLog (@"|  %-31s |", [email UTF8String]);
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|                                  |");
    NSLog (@"|       O                  O       |");
    NSLog (@"====================================");
}
-(AddressCard *) setName: (NSString *) n andEmail: (NSString *) e
{
    self.name = n;
    self.email = e;
    
    return self;
}

@end

AddressBook.h
Code: (Objective-C)
#import <Foundation/NSArray.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;
-(NSMutableArray *) lookup: (NSString *) theName;

@end

AddressBook.m
Code: (Objective-C)
#import "AddressBook.h" 

@implementation AddressBook;

-(id) initWithName: (NSString *) name
{
    self = [self init];

    if (self) {
        bookName = [[ NSString alloc] initWithString: name];
        book = [[NSMutableArray alloc] init];
    }
    
    return self;
}

-(void) addCard: (AddressCard *) theCard
{
    if(![book containsObject: 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];
}

-(NSMutableArray *) lookup: (NSString *) theName
{
    NSMutableArray *result = [NSMutableArray array];
    
    for (AddressCard *nextCard in book)
        if ([[nextCard name] rangeOfString: theName options: NSCaseInsensitiveSearch].length)
            [result addObject: nextCard];
    
    if([result count])
        return result;
        
    return nil;
}

@end

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

int main (int argc, const char * argv[]) {  
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSString  *aName = @"Julia Kochan";
    NSString  *aEmail = @"jewls337@axlc.com";
    NSString  *bName = @"Step Teacher";
    NSString  *bEmail = @"tony.iannino@techfitness.com";
    NSString  *cName = @"Stephen Kochan";
    NSString  *cEmail = @"steve@kochan-wood.com";
    
    AddressCard   *card1 = [[AddressCard alloc] init];
    AddressCard   *card2 = [[AddressCard alloc] init];
    AddressCard   *card3 = [[AddressCard alloc] init];
    
    AddressBook  *myBook = [AddressBook alloc];
    NSMutableArray *searchResult;
    
    [card1 setName: aName andEmail: aEmail];
    [card2 setName: bName andEmail: bEmail];
    [card3 setName: cName andEmail: cEmail];
    
    myBook = [myBook initWithName: @"Linda’s Address Book"];
    
    [myBook addCard: card1];
    [myBook addCard: card2];
    [myBook addCard: card3];
    
    NSLog (@"Lookup: sTEp");
    searchResult = [myBook lookup: @"sTEp"];

    if(!searchResult)
        NSLog(@"No match found!");
    
    else
        for(AddressCard *theCard in searchResult)
                [theCard print];
    
    NSLog (@"Lookup: Nik");
    searchResult = [myBook lookup: @"Nik"];
    
    if(!searchResult)
        NSLog(@"No match found!");
    else
        for(AddressCard *theCard in searchResult)
            [theCard print];
    
    [card1 release];
    [card2 release];
    [card3 release];
    [myBook release];
    
    [pool drain];  
    return 0;  
}
« Last Edit: May 21, 2010, 01:53:09 PM by tadej5553 » Logged
clp274
Newbie
*
Posts: 8


Email




« Reply #1 on: December 13, 2011, 02:02:30 PM »

Hey did you by chance do the same question, but with the indexesOfObjectPassingTest: method. I keep trying it and coming up short. I'm not sure if what I'm doing wrong. Here is my implementation code, but it won't work:

-(NSMutableArray *) lookupAllMatches: (NSString *) theName {
        NSIndexSet *result = [book indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL * stop)
        {
            if ([[obj name] caseInsensitiveCompare:theName] == NSOrderedSame ) {
                return YES; // Found a match, keep going
            } else
                return NO; // Keep looking
       
           
        }];
       
    //Create array to store corresponding objects to the returned Index Set.
    NSMutableArray *matches = [NSMutableArray array];
    //Enumerate through the indexset.
    [result enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)
     {
         //Find all objects in the book array and store those objects that correspond to the Index Set.
         for (Address_Card *aCard in book) {
            [matches insertObject:aCard atIndex:idx];
         }
         
     }];
   //If there is anything found in the matches array, return it. Otherwise, return nill.
    if ([matches count] > 0) {
        return matches;
    }
    return nil;
   
   
}
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.