我正在尝试创建一个数组(States),其中包含多个数组(Cities)。每当我尝试向我的城市数组添加项目时,就会出现以下错误:
'NSInvalidArgumentException',原因:'*** +[NSMutableArray addObject:]:向类0x303097a0发送未识别的选择器
我的代码如下。它发生错误的行是:
'NSInvalidArgumentException',原因:'*** +[NSMutableArray addObject:]:向类0x303097a0发送未识别的选择器
我的代码如下。它发生错误的行是:
[currentCities addObject:city];
我相信我的内存管理存在问题,因为我并不是很理解它。希望有人能够向我解释我的错误。
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];
}
}
currentCities = [currentCities init]
。NSArray的初始化器从不返回接收方,也没有任何类的初始化器保证会返回接收方。 - Chuck