2

I have a class of AddressCards.h/m, and another class of AddressBook,now I want to create a search method to look for names in the AddressBook class but getting some error.

I think I need my method to return an AddressCards type pointer but I'm not sure how to return it, since I need to have an array holding the names in case there is more than 1 match..

This is my current code:

  -(NSMutableArray *) searchName:(NSString *) someName{

        NSMutableArray *results = [NSMutableArray alloc];

        for (NSString *name in book)
        {
            if ([someName caseInsensitiveCompare:name] == NSOrderedSame)
                [results addObject:name];
        }
        return results;
    }

@end

I'm getting error in this line: if ([someName caseInsensitiveCompare:name] == NSOrderedSame) That says signal 1 SIGABRT which I have no idea what is it :/

this is the method that adds addresscards:

-(void) addCard:(AddressCards *)theCard{

    [book addObject:theCard];
}
JohnBigs
  • 2,691
  • 3
  • 31
  • 61
  • How is book being initialized? Also SIGABRT just means an abort signal has been thrown. You should set a break point and inspect for more information – Jason Sperske Mar 04 '13 at 22:31
  • @JasonSperske thanks. this is book '@property (nonatomic, strong) NSMutableArray *book;' – JohnBigs Mar 04 '13 at 22:33
  • @JasonSperske and i added to the question the method that adds addresscards – JohnBigs Mar 04 '13 at 22:36
  • 1
    I would recommend being careful using `caseInsensitiveCompare:`. `NSOrderedSame` is defined as 0. If `someName` is `nil` (which evaluates to 0 also), you can get a false positive. I would rewrite your condition to `if ( someName != nil && [someName caseInsensitiveCompare:name] == NSOrderedSame )`. – Mike D Mar 04 '13 at 22:48

2 Answers2

3

One problem is that you haven't called init when creating your results array.

NSMutableArray *results = [NSMutableArray alloc];

should be:

NSMutableArray *results = [[NSMutableArray alloc]init];

This will definitely cause a SIGABRT error.

Rich Schonthal
  • 472
  • 2
  • 12
3

book is an NSMutableArray of AddressCards, however you are trying to iterate over the objects using an NSString (name).

You should iterate using addressCards object and then make the comparison using the corresponding name property. I believe you should implement the searching method similar to the code below:

-(NSMutableArray *) searchName:(NSString *) someName{

    NSMutableArray *results = [[NSMutableArray alloc] init];

    for (AddressCards *addressCard in book)
    {
        // do your comparison check here
        // assuming that you have a name property in AddressCards class

        if ([addressCard.name rangeOfString:someName].location != NSNotFound)
            [results addObject:addressCard.name];
    }
    return results;
}

Hope this helps.

Edit: Modified the comparison code as desired using this answer.

Community
  • 1
  • 1
tolgamorf
  • 809
  • 6
  • 23
  • 1
    I was just writing this same answer, but I refreshed and saw you already wrote it :) – Mike C. Mar 04 '13 at 22:49
  • @tolgamorf thanks man :) how would you test it in the main? after calling [someAddressBook searchNam:@"some name"]; – JohnBigs Mar 04 '13 at 22:56
  • @tolgamorf something else I would like to ask if you dont mind, is why if im writing just part of the name like "john" instead of "john bigs" it wont work..isn't it suppose to? – JohnBigs Mar 04 '13 at 22:59
  • just add an NSLog before returning the results. NSLog(@"results array: %@", results); – tolgamorf Mar 04 '13 at 22:59
  • @JohnBigs it is due to the comparison method you are using. You should check if someName is a substring of addressCard.name – tolgamorf Mar 04 '13 at 23:05
  • @JohnBigs You can find how to do it [here](http://stackoverflow.com/a/2754075/1576979). – tolgamorf Mar 04 '13 at 23:06
  • 1
    @JohnBigs you're welcome, I updated the code replacing the comparison method. – tolgamorf Mar 04 '13 at 23:12