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];
}
4
  • 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 Commented Mar 4, 2013 at 22:31
  • @JasonSperske thanks. this is book '@property (nonatomic, strong) NSMutableArray *book;' Commented Mar 4, 2013 at 22:33
  • @JasonSperske and i added to the question the method that adds addresscards Commented Mar 4, 2013 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 ). Commented Mar 4, 2013 at 22:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

I was just writing this same answer, but I refreshed and saw you already wrote it :)
@tolgamorf thanks man :) how would you test it in the main? after calling [someAddressBook searchNam:@"some name"];
@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?
just add an NSLog before returning the results. NSLog(@"results array: %@", results);
@JohnBigs you're welcome, I updated the code replacing the comparison method.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.