NSSet 到 NSArray 调用 objectAtIndex 转换?

发布于 2024-12-28 10:38:51 字数 825 浏览 1 评论 0原文

我试图通过删除可见区域之外的所有注释,并添加和删除可见区域内的一些注释来更新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情徒 2025-01-04 10:38:51

尽管您将 NSMutableSet 转换为 NSArray,但这种简单的转换不会使 NSSet 类响应 NSArray'的消息。您必须使用 NSSet 的元素填充实际的 NSArray,如下所示:

NSArray *array = [theNsSet allObjects];

Despite you're casting NSMutableSet to NSArray, that simple casting won't make NSSet class respond to NSArray's messages. You have to fill an actual NSArray with the elements of the NSSet like this:

NSArray *array = [theNsSet allObjects];
メ斷腸人バ 2025-01-04 10:38:51

NSSet 对象转换为 NSArray 不会做任何其他事情来欺骗编译器认为该对象是 NSArray。实际上,该对象一个NSSet对象,尝试将其用作NSArray将会失败。

另一种看待它的方式是,强制转换只是针对指针的一个技巧,而不是针对保持不变的指向对象的技巧。

强制转换仅在某些情况下是安全的,例如从派生类强制转换为基类时;或者当您绝对确定底层对象的实际类型与您要转换为的类型一致时。

无论如何,在您的具体情况下,您可以尝试使用以下方法通过 NSArray 访问 NSSet 元素:

 [newAnnotations allObjects]

这个

返回包含集合成员的数组,如果集合没有成员,则返回空数组。

Casting an NSSet object to NSArray will not do anything else that tricking the compiler into thinking that the object is an NSArray. Actually, the object is an NSSet object and trying to use it as an NSArray 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:

 [newAnnotations allObjects]

This

Returns an array containing the set’s members, or an empty array if the set has no members.

沐歌 2025-01-04 10:38:51

从 NSSet 获取 NSMutableArray 的另一种方法是

NSMutableArray * array= [[set allObjects] mutableCopy];

另外, satzkmr 提出的解决方案会给您一个“不兼容的指针”警告。 (很抱歉在这里写下这个,我没有足够的声誉来发表评论)。

Another way to get an NSMutableArray from an NSSet is

NSMutableArray * array= [[set allObjects] mutableCopy];

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).

悍妇囚夫 2025-01-04 10:38:51

是的,您应该首先将集合存储到这样的数组中......

NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];

Yes, you should first store the set into an array like this...

NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]];
御弟哥哥 2025-01-04 10:38:51

将 NSSet 转换为 NSArray 或 NSMutableArray 的步骤如下:

NSSet *airports = [NSSet setWithObjects:@"Chennai",@"Mumbai",@"Delhi", nil];
NSLog(@"Set Elemets Before Move:%@", airports);
NSMutableArray *movedAirports = [airports allObjects];
NSLog(@"Array Elements After Moving from Set:%@", movedAirports);

Following Step to be followed to convert a NSSet into NSArray or NSMutableArray,

NSSet *airports = [NSSet setWithObjects:@"Chennai",@"Mumbai",@"Delhi", nil];
NSLog(@"Set Elemets Before Move:%@", airports);
NSMutableArray *movedAirports = [airports allObjects];
NSLog(@"Array Elements After Moving from Set:%@", movedAirports);
策马西风 2025-01-04 10:38:51
NSArray *array = [sets allObjets];
NSArray *array = [sets allObjets];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文