Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:20:48 AM *
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
  Print  
Author Topic: Question on 15.3  (Read 1414 times)
jefflebl
Newbie
*
Posts: 8


« on: April 15, 2009, 10:10:06 AM »

Hi Stephen and group,

Attached is my attempt at 15.3 (doesn't include all methods from Chapter 15, but just up through lookup).  I am receiving a warning at the line

matches = [myBook lookup: @"gmail"];

The warning is "assignment from distinct Objective-C type".  I'm also receiving a warning within the definition for lookup.

I'm sure I'm missing something silly here, but any help you could provide would be much appreciated.

Thanks,
Jeff

-------------
AddressCard.h
Code:

#import <Foundation/Foundation.h>

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

@property (copy, nonatomic) NSString *name, *email;
-(void) print;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(void) dealloc;
@end

--------------
AddressCard.m
Code:

#import "AddressCard.h"

@implementation AddressCard

@synthesize name, email;

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

-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail
{
   name = theName;
   email = theEmail;
}

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

@end

----------------
Addressbook.h
Code:

#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;

@end

------------------
Addressbook.m
Code:

#import "Addressbook.h"

@implementation Addressbook

-(id) initWithName: (NSString *) name
{
   self = [super init];
   
   if (self)
   {
      bookName = [[NSString alloc] initWithString: name];
      book = [NSMutableArray new];
   }
   
   return self;
}

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

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

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

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

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

-(NSMutableArray *) lookup: (NSString *) theName
{
   NSRange compare1, compare2;
   NSMutableArray *matches = [NSMutableArray new];
   
   for (AddressCard *nextCard in book)
   {
      compare1 = [nextCard.name rangeOfString: theName];
      compare2 = [nextCard.email rangeOfString: theName];
      
      if((compare1.location != NSNotFound) || (compare2.location != NSNotFound))
         [matches addObject: nextCard];
   }
   
   if (matches.count < 1)
      return nil;
   
   return matches;
}
   
@end

---------------------
Main code:

// Working with Address Cards and Address Books

#import "AddressCard.h"
#import "Addressbook.h"

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

   NSString *aName = @"Joe Blow";
   NSString *aEmail = @"joe.blow@gmail.com";
   NSString *bName = @"Jane Doe";
   NSString *bEmail = @"jane.doe@gmail.com";
   NSString *cName = @"Joe Blow, Jr.";
   NSString *cEmail = @"joe.blowjr@comcast.net";
   NSString *dName = @"Suzie Doe";
   NSString *dEmail = @"suzie.doe@comcast.net";
   
   AddressCard *card1 = [AddressCard new];
   AddressCard *card2 = [AddressCard new];
   AddressCard *card3 = [AddressCard new];
   AddressCard *card4 = [AddressCard new];
   
   Addressbook *myBook = [Addressbook new];
   NSMutableArray *matches;
   
   // try different setting methods for practice
   
   [card1 setName: aName andEmail: aEmail];
   [card2 setName: bName andEmail: bEmail];
   card3.name = cName; card3.email = cEmail;
   card4.name = dName; card4.email = dEmail;
   
   // now set up the Address Book
   
   myBook = [myBook initWithName: @"Jeff's Address Book"];
   NSLog (@"Entries in address book after creation: %i", [myBook entries]);
          
   // add some entries ...
           [myBook addCard: card1];
           [myBook addCard: card2];
           [myBook addCard: card3];
           [myBook addCard: card4];
          
   // check to see how many entries are there now
           NSLog (@"Entries in address book now: %i", [myBook entries]);

   // now try lookup function ...
   
   NSLog (@"Lookup: gmail");
   matches = [myBook lookup: @"gmail"];
   
   if (matches != nil)
   {       
   for (AddressCard *matchCard in matches)
           NSLog(@" -20s%   %-32s", [matchCard.name UTF8String], [matchCard.email UTF8String]);   
   }
   else NSLog(@"No match found for gmail");
         
   
   // print ...
   [card1 print]; [card2 print]; [card3 print]; [card4 print];
   
          
   // release all
   [card1 release]; [card2 release]; [card3 release]; [card4 release];
           [myBook release]; [matches release];
   
    [pool drain];
    return 0;
}


Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: April 15, 2009, 10:27:43 AM »

This is likely the line that is causing your problems:

Code: (Objective-C)
-(AddressCard *) lookup: (NSString *) theName;

I think you know why.    Wink

Cheers,

Steve Kochan
Logged
jefflebl
Newbie
*
Posts: 8


« Reply #2 on: April 15, 2009, 10:40:42 AM »

Ahhhhhh.  Thanks so much for the prompt reply, Steve! 

Best,
Jeff
Logged
Pages: [1]   Go Up
  Print  
 
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 classroomM.com. All rights reserved.