这个Objective-C代码我错在哪里?

时间:2015-05-24 08:25:45

标签: ios objective-c arrays

我是Obejective-C的新手,这是第15章编程的Objective-C第六版中的练习,我想知道我在错误的地方查找,当我调试程序时,它会跳转此代码 [resultOfLookup addObject:nextCard]; 我不知道为什么。请帮助我,谢谢。 // 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;
}

1 个答案:

答案 0 :(得分:0)

您似乎没有分配和初始化班级resultOfLookup的{​​{1}}属性。尝试在AddressBook的初始化过程中添加此行。

AddressBook