objective-c - 使用 CLLocation 对象作为字典中的键

标签 objective-c ios4 mapkit core-location

是否可以使用 CLLocation 对象作为字典中的键?当我尝试这个时, [dictionary objectForKey: clLocationObj] 总是返回 nil ,即使已经插入了具有完全相同的纬度和经度的 CLLocation 键。我做错了什么?

       for (Location *location in someArray) {
               CLLocation *locationKey = [[CLLocation alloc] initWithLatitude:[location.latitude doubleValue] longitude:[location.longtitude doubleValue]];
               LocationAnnotation *annotationAtLocation = [self.uniqueLocations objectForKey:locationKey];
               if (annotationAtLocation == nil)
                    NSLog(@"this is always nil");
       }

通过调试得知someArray中有多个Location的对象具有相同的经纬度。

最佳答案

CLLocation 似乎没有覆盖 isEqual 来执行实际的内容比较,而是根据对象标识比较相等性。因此,将它用作字典中的键是不明智的,除非您总是使用完全相同的对象访问它。

您在评论中描述的解决方案在许多情况下都是很好的解决方法:

I ended up converting the CLLocationCoordinate2D object into an NSValue and used that as the key to the dictionary.

关于objective-c - 使用 CLLocation 对象作为字典中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6771508/

相关文章:

ios - 如何像 Apple 的 Maps.app 一样在 MKMapView 缩放期间显示 map 比例

swift - 为什么我无法在 map 上显示我的注释?

iphone - 在 map 上显示用户位置并停止位置

iphone - 如何捕获 EXC_BAD_ACCESS (code=1, address=0x00XXXXXX)?

iphone - iPhone SDK:可以通过iPad设备访问Localhost吗? (调试时)

iphone - 将字符串转换为日期的日期比较

iphone - iOS使用NSDictionary将数据加载到节和行中

objective-c - 当tableview从URL获取数据时获取EXC_BAD_ACCESS?

objective-c - AFNetworking 发布请求未通过 Sinatra 应用程序

iphone - 如何绘制具有特定纹理(如粉笔)的线条?