我正在尝试创建数组(城市)的数组(州)。每当我尝试将项目添加到城市数组时,我都会收到此错误:
'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];
}
}
线路:
// set up a new cities array
currentCities = [NSMutableArray init];
应该读:
// set up a new cities array
[currentCities init];
这应该可以解决你的问题。您不是初始化数组,而是向类对象发送一条初始化消息,该消息不执行任何操作。之后,你的currentCities指针仍然没有初始化。
更好的方法是删除该行并更改第四行,以便您一步即可分配和初始化所有内容:
NSMutableArray *currentCities = [[NSMutableArray alloc] init];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)