ios NSSet 到 NSArray 转换调用 objectAtIndex?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8985053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:27:30  来源:igfitidea点击:

NSSet to NSArray casting calling objectAtIndex?

objective-ciosnsarraymkmapviewnsset

提问by benwad

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:

我试图通过删除可见区域外的所有注释并添加和删除可见区域内的一些注释来更新 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];

This gives me the error -[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x13cd40after 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?

这给了我错误-[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x13cd40在我将 newAnnotations 转换为 NSArray 然后添加注释的最后一行之后。是否有将数组强制转换为导致此问题的集合?如果是这样,有没有办法绕过它?

回答by

Despite you're casting NSMutableSetto NSArray, that simple casting won't make NSSetclass respond to NSArray's messages. You have to fill an actual NSArraywith the elements of the NSSetlike this:

尽管您正在投射NSMutableSetNSArray,但这种简单的投射不会使NSSet班级响应NSArray的消息。你必须NSArray用这样的元素填充一个实际的NSSet

NSArray *array = [theNsSet allObjects];

回答by sergio

Casting an NSSetobject to NSArraywill not do anything else that tricking the compiler into thinking that the object is an NSArray. Actually, the object isan NSSetobject and trying to use it as an NSArraywill produce failure.

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

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:

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

 [newAnnotations allObjects]

This

这个

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

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

回答by ACBM

Another way to get an NSMutableArray from an NSSet is

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

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

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

回答by Nishant

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

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

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

回答by satzkmr

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

以下步骤将 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);

回答by wq jiang

NSArray *array = [sets allObjets];