NSMutableArray addObject, unrecognized selector

I am trying to create an array (state) of arrays (cities). Whenever I try to add an item to my City array, I get this error:

'NSInvalidArgumentException', reason: '*** + [NSMutableArray addObject:]: unrecognized selector sent to class 0x303097a0

My code is as follows. The line on which he makes mistakes is

 [currentCities addObject:city];

I am sure that I have a memory management problem as I still do not understand all this. I was hoping someone could explain my mistake to me.

if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK){
        // We need to keep track of the state we are on
        NSString *state = @"none";
        NSMutableArray *currentCities = [NSMutableArray alloc];

        // We "step" through the results - once for each row
        while (sqlite3_step(statement) == SQLITE_ROW){
            // The second parameter indicates the column index into the result set.
            int primaryKey = sqlite3_column_int(statement, 0);
            City *city = [[City alloc] initWithPrimaryKey:primaryKey database:db];

            if (![state isEqualToString:city.state])
            {
                // We switched states
                state = [[NSString alloc] initWithString:city.state]; 

                // Add the old array to the states array
                [self.states addObject:currentCities];

                // set up a new cities array
                currentCities = [NSMutableArray init];
            }

            [currentCities addObject:city];
            [city release];
        }
    }
+5
source share
3 answers

Rows:

// set up a new cities array
currentCities = [NSMutableArray init];

Must read:

// set up a new cities array
[currentCities init];

, , . init , . currentCities .

4- , :

NSMutableArray *currentCities = [[NSMutableArray alloc] init];
+9

- NSMutableArray, ? initWithCapacity, - ? , , .

** . [[NSMutableArray alloc] init], .

+3

.

NSMutableArray *myArray  = [NSMutableArray mutableCopy]; // not initialized.  don't know why this even compiles
[myArray addObject:someObject];  // crashed

to

NSMutableArray *myArray  = [NSMutableArray new]; // initialized!
+1

All Articles