gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:40:51 24 4
gpt4 key购买 nike

是否可以使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com