在while循环中添加到NSDictionary

时间:2012-02-20 08:33:58

标签: objective-c xcode nsdictionary

我正在向地图添加注释,为了调用它们,我需要将它们的数据库ID值存储在字典中

int i = 0;
...

while(sqlite3_step(statement) == SQLITE_ROW) {   

     int thisvenueid = sqlite3_column_int(statement, 4);
     NSNumber *vid = [[NSNumber alloc] initWithInt:thisvenueid];

      if (dict == nil) {
           dict = [[NSMutableDictionary alloc] init];
      }
      [dict setObject:vid forKey:[NSNumber numberWithInt:i]];
}

然后我就像这样使用它:

[self.mapView selectAnnotation:[dict objectForKey:selectedVenue] animated:YES];

然而,这只是崩溃(Sigbart ......我不喜欢Sigbart)。我非常感谢任何建议。

2 个答案:

答案 0 :(得分:0)

您为什么使用setObject?从它看起来,你应该使用addObject

[dict addObject: vid forKey: [NSNumber numberWithInt:i]];

如果这不是问题,请原谅我 - 暂时没有使用Objective-C。

答案 1 :(得分:0)

您正在尝试选择注释并传递NSNumber而不是MKPlacemark的对象

请参阅文档:

<强> selectAnnotation:动画: 选择指定的注释并为其显示一个标注视图。

- (void)selectAnnotation:(id < MKAnnotation >)annotation animated:(BOOL)animated

参数 的 * 注释 * 要选择的注释对象 的动画 如果是,则将标注视图设置为动画到位 的讨论 如果指定的注释不在屏幕上,因此没有关联的注释视图,则此方法无效。