是否可以使用 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 anNSValue
and used that as the key to the dictionary.
关于objective-c - 使用 CLLocation 对象作为字典中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6771508/