NSSet 到 NSArray 调用 objectAtIndex 转换?
我试图通过删除可见区域之外的所有注释,并添加和删除可见区域内的一些注释来更新 MKMapView。这是我的代码:
NSSet *visibleAnnotations = [mapView annotationsInMapRect:[mapView visibleMapRect]];
NSSet *allAnnotations = [NSSet setWithArray:[mapView annotations]];
NSMutableSet *nonVisibleAnnotations = [NSMutableSet setWithSet:allAnnotations];
[nonVisibleAnnotations minusSet:visibleAnnotations];
[mapView removeAnnotations:(NSArray *)nonVisibleAnnotations];
NSMutableSet *newAnnotations = [NSMutableSet setWithArray:[_zoomLevels objectAtIndex:clusterLevel]];
[newAnnotations minusSet:visibleAnnotations];
[mapView addAnnotations:(NSArray *)newAnnotations];
在我将 newAnnotations 转换为 NSArray 然后添加注释的最后一行之后,这给了我错误 -[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x13cd40 。将数组强制转换为集合是否会导致此问题?如果是这样,有办法解决吗?
I'm trying to update an MKMapView by removing all annotations outside the visible area, and adding and removing some annotations inside the visible area. This is my code:
NSSet *visibleAnnotations = [mapView annotationsInMapRect:[mapView visibleMapRect]];
NSSet *allAnnotations = [NSSet setWithArray:[mapView annotations]];
NSMutableSet *nonVisibleAnnotations = [NSMutableSet setWithSet:allAnnotations];
[nonVisibleAnnotations minusSet:visibleAnnotations];
[mapView removeAnnotations:(NSArray *)nonVisibleAnnotations];
NSMutableSet *newAnnotations = [NSMutableSet setWithArray:[_zoomLevels objectAtIndex:clusterLevel]];
[newAnnotations minusSet:visibleAnnotations];
[mapView addAnnotations:(NSArray *)newAnnotations];
This gives me the error -[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x13cd40 after the final line in which I cast newAnnotations to an NSArray then add the annotations. Is there something about casting an array to a set that causes this? If so, is there a way round it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尽管您将
NSMutableSet
转换为NSArray
,但这种简单的转换不会使NSSet
类响应NSArray
'的消息。您必须使用NSSet
的元素填充实际的NSArray
,如下所示:Despite you're casting
NSMutableSet
toNSArray
, that simple casting won't makeNSSet
class respond toNSArray
's messages. You have to fill an actualNSArray
with the elements of theNSSet
like this:将
NSSet
对象转换为NSArray
不会做任何其他事情来欺骗编译器认为该对象是NSArray
。实际上,该对象是一个NSSet
对象,尝试将其用作NSArray
将会失败。另一种看待它的方式是,强制转换只是针对指针的一个技巧,而不是针对保持不变的指向对象的技巧。
强制转换仅在某些情况下是安全的,例如从派生类强制转换为基类时;或者当您绝对确定底层对象的实际类型与您要转换为的类型一致时。
无论如何,在您的具体情况下,您可以尝试使用以下方法通过 NSArray 访问 NSSet 元素:
这个
Casting an
NSSet
object toNSArray
will not do anything else that tricking the compiler into thinking that the object is anNSArray
. Actually, the object is anNSSet
object and trying to use it as anNSArray
will produce failure.Another way to see it is that casting is just a trick on pointers, not on the pointed-to objects, that remain unaltered.
Casting is only safe in certain cases, like when you cast from a derived class to a base class; or when you are absolutely sure that the underlying object real type is consistent with the type you are casting it to.
Anyway, in your specific case, you might try to access the NSSet elements through an NSArray by using:
This
从 NSSet 获取 NSMutableArray 的另一种方法是
另外, satzkmr 提出的解决方案会给您一个“不兼容的指针”警告。 (很抱歉在这里写下这个,我没有足够的声誉来发表评论)。
Another way to get an NSMutableArray from an NSSet is
Also, the solution proposed by satzkmr gives you an "Incompatible pointer" warning. (Sorry to write this here, I don't have enough reputation to comment).
是的,您应该首先将集合存储到这样的数组中......
Yes, you should first store the set into an array like this...
将 NSSet 转换为 NSArray 或 NSMutableArray 的步骤如下:
Following Step to be followed to convert a NSSet into NSArray or NSMutableArray,