Objective-C >> Using for-in Loop >> Making the Loop Enter Only if the Object is of a Certain Type

StackOverflow https://stackoverflow.com/questions/17591609

Question

I was thinking... When we use a for-in loop we are supposed to set the type of the object searched in the collection.

int sumNums = 0;
for (MYObject* myObj in simpleArray) {

    sumNums += myObj.myNum;

}

But... if simpleArray has different types of objects and not just objects of type MYObject, I would expect that the loop will "skip" them; instead, I've noticed that it converts them to a MYObject object and that makes no sense (and of course, causes the app to crash).

Is there a way to make myObj only enter the loop code lines if the object is of that specific type? or am I doomed to go over all the objects (and then have to start introspecting them, etc...)?

Était-ce utile?

La solution 2

Just check for the correct class yourself, by using isKindOfClass:

for (MYObject* myObj in simpleArray) {
    if (![myObj isKindOfClass:[MYObject class]]) {
        // wrong class. continue to next object.
        continue;
    }
    sumNums += myObj.myNum;
}

Autres conseils

if simpleArray has different types of objects and not just objects of type MYObject, I would expect that the loop will "skip" them;

At run time, objects are just objects. The philosophy of object oriented programming á la Objective-C/Smalltalk is that you send messages to objects and it decides what to do with them. The static type of the object is irrelevant and, in fact, unknown.

What you have asked the runtime to do is iterate through the objects in the array and send the message -myNum to each one. It is not allowed to filter out other object types, because they might also respond to the selector e.g. you could add a category to NSString that declares the selector.

instead, I've noticed that it converts them to a MYObject object and that makes no sense

It doesn't convert them at all, it just sends the message. If you want to stop this from crashing the application, you can do what the other answers suggest and test that you have an object of the correct class, but it might be more philosophically appropriate to test if it responds to the selector e.g.

int sumNums = 0;
for (MYObject* myObj in simpleArray) {

    if ([myObj respondsToSelector: @selector(myNum)])
    {
        sumNums += [myObj myNum]; // Using message sytax to make it lear that properties are just messages.
    }
}
int sumNums = 0;
for (NSObject* myObj in simpleArray) {
    if([myObj isKindOfClass:[MyObject class]])
       sumNums += myObj.myNum;

}

You will have to specifically check class of each object, specifying a type in for-in won't work as you want.

MYObject *myObj will just make the pointer of that type, wont ensure that the object is actually of that type.

Using isKindOfClass will check if the object belongs to a particular class or its subclass.

Note: In case you wish to match the type exactly and not include objects of subclasses then instead of isKindOfClass, you can use isMemberOfClass.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top