Method Creates an array with 11 objects, all out of scope, the results of an unrecognized selector

So, I am doing a simple search. I have an array of NSString objects and a string to search in the elements of the array.

Everything seems to work until I try to add a match to a new mutable array made to store the search results. The variable stringHolder receives the string, and resultCollectorArray even gets the correct number of new elements, but each element is empty and "out of range". Here's the method:

@implementation NSArray (checkForString)

-(NSMutableArray *) checkForString: (NSString *) matchSought
{
    long unsigned numberofArrayElements;
    long unsigned loop = 0;
    NSRange searchResults;
    NSMutableArray * resultCollectorArray = [[NSMutableSet alloc] init];
    id stringHolder;

    numberofArrayElements = [self count];

    while (loop < numberofArrayElements) {
        searchResults.length = 0;
        searchResults = [[self objectAtIndex: loop] rangeOfString: matchSought options:NSCaseInsensitiveSearch];
        if (searchResults.length > 0) {
            stringHolder = [self objectAtIndex: loop];
            [resultCollectorArray addObject: stringHolder];
        }
        loop++;
    }

    return [resultCollectorArray autorelease];

}

As soon as we get back to the main part of the program, I get an unrecognized selector sent to a mutable array that should have received the result of the method. Here's the main section:

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

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString *testString = [[NSString alloc] initWithString: @"ab"];
    NSMutableString * resultString = [[NSString alloc] init];
    NSArray * theArray = [[NSArray alloc] initWithObjects: ..., nil];  // Actual code has the objects
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];

    NSUInteger arrayCount = 0;
    unsigned long loops = 0;


    resultArray = [theArray checkForString: testString];
    arrayCount = [resultArray count];

        while (loops < arrayCount){
            resultString = [resultArray objectAtIndex: loops];  // Here where we get the unrecognized selector.
            NSLog(@"%@", resultString);
            loops++;
    }


    [pool drain];  // Also, I'll release the objects later. I just want to get what above working first. 
    return 0;
}

(), , .

.

.

+3
1

NSMutableArray * resultCollectorArray = [[NSMutableSet alloc] init]; . .

, objectAtIndex: NSMutableSet. ,

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

NSArray * filteredArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchString]];

. , .:)

+8

All Articles