我在mapView中有4-5种不同的注解类。对于下面的代码,我希望只有AnnotationType1会响应for循环。
for (AnnotationType1* annotation in mymap.annotations)
{
NSLog(@"annotation class is %@", [annotation class]);
}但从console中可以明显看出,我还得到了其他类。
annotation class is AnnotationType1
annotation class is AnnotationType2
annotation class is AnnotationType3
annotation class is AnnotationType4仅在AnnotationType1注解上执行操作的最佳方式是什么?
发布于 2012-01-07 14:24:40
首先,正如你已经发现的,快速迭代并不像你想象的那样工作。不管是什么,mymap.annotations都会返回相同的注释对象数组--它根本不知道您将它们赋给了哪种类型的指针。
其次,依靠视图(比如MKMapView)来存储数据(比如您的注释)通常被认为是一个坏主意。地图视图知道注释是很好的--它必须知道它们才能正确地执行其工作。但我不建议依靠地图视图来维护应用程序的状态。您可能已经将注释对象存储在数据模型中的某个位置--如果是这样的话,这将是获取注释列表的更好位置。
第三,可以使用谓词过滤数组。使用谓词按类名过滤的帮助信息为See this answer。
https://stackoverflow.com/questions/8767758
复制相似问题