Where I’m wrong in this oc code?

I’m a newbie for Obejective-C this is a exercise in chapter 15 in Programming in Objective-C Sixth Edition , I would like to know where I’m wrong in founction lookup ,when I debug the programme it jump this code [resultOfLookup addObject:nextCard]; I don’t know why .please help me , Thanks.
// AddressBook.h

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

@interface AddressBook : NSObject

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

-(instancetype) initWithName:(NSString *) name;
-(void) addCard: (AddressCard *) theCard;
-(int) entries;
-(void) list;
-(NSMutableArray *) lookup:(NSString *) theName;

@end

// AddressBook.m

#import "AddressBook.h"

@implementation AddressBook
@synthesize bookName,book,resultOfLookup;



-(instancetype) initWithName:(NSString *)name
{
    self =[super init];
    if (self) {
        bookName = [NSString stringWithString:name];
        book=[NSMutableArray array];
    }
    return self;
}

-(instancetype) init:(NSString *)name
{
    return [self initWithName:@"NoName"];
}

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

-(NSMutableArray *) lookup:(NSString *) theName
{
    for (AddressCard * nextCard in book) {
        if ([nextCard.name localizedCaseInsensitiveCompare:theName] == NSOrderedSame) {
            [resultOfLookup addObject:nextCard];
        }
    }
    return resultOfLookup;
}
-(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(@"=======================================================");
}

@end

// AdressCard.h

#import <Foundation/Foundation.h>

@interface AddressCard : NSObject

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

-(void) print;

@end

// AdressCard.m

#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(@"========================================");

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

@end

// main.m

#import "AddressBook.h"

int main(int argc, const 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";

        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 Address Book"];
       // AddressCard *myCard=[[AddressCard alloc] init];
        NSMutableArray *lookupResult=[[NSMutableArray alloc] init];

        //NSLog(@"Entries in address book after creation: %i",[myBook entries]);
        [card1 setName:aName andEmail: aEmail];
        [card2 setName:bName andEmail:bEmail];
        [card3 setName:cName andEmail:cEmail];
        [card4 setName:dName andEmail:dEmail];

        [myBook addCard:card1];
        [myBook addCard:card2];
        [myBook addCard:card3];
        [myBook addCard:card4];

        NSLog(@"Stephen Kochan");
        lookupResult=[myBook lookup:@"Stephen Kochan"];

        NSLog(@"%@",lookupResult);
        //NSLog(@"======== Contents of: %@ =========", bookName);

        for (AddressCard * theCard in lookupResult) {
            NSLog(@"%-20s   %-32s", [theCard.name UTF8String],[theCard.email UTF8String]);
        }
        NSLog(@"=======================================================");
       /*

        NSLog(@"Haibo Zhang");
        myCard = [myBook lookup:@"Haibo Zhang"];

        if (myCard!=nil) {
            [myCard print];
        }
        else
        {
            NSLog(@"Not Found");
        }
        */



        //NSLog(@"Entries in address book after adding cards: %i",[myBook entries]);

       // [myBook list];


        // insert code here...
        //NSLog(@"Hello, World!");
    }
    return 0;
}


Source: ios

Leave a Reply