NSSet to NSArray casting calling objectAtIndex?

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?

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];

objective-c, Nonostante stai lanciando NSMutableSet - NSArray , cos� semplice fusione non far� NSSet classe rispondere ai messaggi NSArray s'. � necessario compilare� To receive the latest developer news, visit and subscribe to our News and Updates.

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.

objective-c, A pesar de que estas lanzando NSMutableSet a NSArray , que la fundici�n simple no har� NSSet responden a los mensajes de clase NSArray 's. Usted tiene� @NSManaged var types : NSSet when creating a NSManagedObject subclass from the editor. So I was wondering if there was a not very verbose method to use the filter method without casting. But then Swift is supposed to bridge automatically between NSSet, NSArray and their counterparts. So I tried declaring it directly as a Set and it works.

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

objective-c - NSSet到NSArray转换调用objectAtIndex吗?, NSSet to NSArray casting calling objectAtIndex? 发表于 2012-01-24 10:13:27. 活跃于 2019-11-27 14:49:33. 查看48542 次. objective-c ios nsarray mkmapview� Before making a custom subclass of NSArray, investigate NSPointer Array and the corresponding Core Foundation type, CFArray. Because NSArray and CFArray are “toll-free bridged,” you can substitute a CFArray object for a NSArray object in your code (with appropriate casting).

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

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

When is it better to use an NSSet over an NSArray?, NSSet to NSArray casting calling objectAtIndex?, Despite you're casting NSMutableSet to NSArray , that simple casting won't make NSSet class respond to� But also, be aware that calling -count each time through the array can cancel out the benefit of using -objectAtIndex: – Quinn Taylor Jun 14 '09 at 14:31 1 And in reality, if you're enumerating a mutable collection, it is technically more correct to check the count every time through the loop.

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

When is it better to use an NSSet over an NSArray?, which is faster to iterate through an nsarray or an nsset nsset to nsarray nsset in swift nsset example nsset vs nsdictionary nsset objectatindex how to access� NSSet uses those two methods to test objects; if you redefine isEqual: to compare the values contained in the MapAnnontation objects, rather than the identity of the objects themselves, the set will take them to be equal.

User benwad, 46 NSSet to NSArray casting calling objectAtIndex? 38 "Implicit instantiation of undefined template" when forward declaring template class � 25 Get member of� I have some NSDictionary objects stored in an NSArray called telephoneArray. I fetch the values for the key number and then replace the NSDictionary I've just read with a new object at the same index in the array. I then want to put these new objects into an NSSet. How can this be achieved? See my unsuccessful attempt below.

User benwad, NSSet to NSArray casting calling objectAtIndex? objective-c ios nsarray mkmapview nsset asked Jan 24 '12 at 10:13. stackoverflow.com. 36 votes� Manage iOS App Layouts with Autolayout. Ios. Animated Components with UIKit Dynamics: Part 1. Getting Started with Auto Layout in Xcode 5. Objective c. Iphone

Control Structures, Loops and Collections – How To Make iPhone , Collections: NSArray, NSSet and NSDictionary If we know for a fact that the object at index 0 is a Superhero object, then we can use Casting to assign it to the� -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance I just want to log the value 1021 in the first array of the first array. objective-c xcode indexing nsarray

Comments
  • allObjects changes the order.So objectAtIndex may not return the intended.
  • @Vignesh Non sequitur. A set is not ordered.
  • The suggested solution here doesn't work for me... [theNSSet allObjects] simply adds the NSSet as an object inside an NSArray, which is a fail, because I don't need an NSSet INSIDE an NSArray, I need an NSArray FROM the contents of the NSSet
  • @LogicsaurusRex allObjects returns an Array containing the Set's members. This solution should work for you.