I have set up a Core Data model that includes an entity, Item with a 1->M relationship with the abstract entity Place, so that an item has many places. There are several entities with the parent set to Place.
I want to set up several UI elements depending on the descendent place types. I have a loop that looks something like this:
for (Place *place in item.places) {
}
… but I’m not sure how to detect what type the place is, and how to cast it to the proper type so that I can access its properties.
Thanks for any help!
Not entirely sure what you are asking, but sounds like you have a collection of objects which are of subclasses of Place, and you need to detect the concrete type at runtime.
Here’s how you do the branching and casting:
Be sure to look at docs for isKindOfClass: and isMemberOfClass: in NSObject reference to understand the difference, you can use either depending on the circumstances.
(You can substitute “id object” with “Place *object”, I was just using id in my code. — edit: or maybe you can’t if it’s abstract, see mzarra’s comment. “id” works fine.)