NSSet to NSArray casting calling objectAtIndex?

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.


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