Newbee
Newbee

Reputation: 3301

Writing a for loop in Objective C way

I have a doubts in writing a for loop in Objective C way. I can do the same loop in traditional C for loop, However I am trying to learn Objective C. Here is my question.
listdata is a mutable array holding objects of ofi_vc_modal_ab_user_info objects, I want to compare each email of list data with email until list data count and find its position and if found I want to delete the object from list data.

    for (ofi_vc_modal_ab_user_info *loc_obj in listData)
    {
        if (strcasecmp(loc_obj->email, email) == 0) {
           // What need to do here.
        }
    }

How to proceed here... thanks for your helps :)

Upvotes: 0

Views: 206

Answers (2)

BornCoder
BornCoder

Reputation: 1066

BOOL foundObject = NO; 
ofi_vc_modal_ab_user_info *loc_found_obj = nil; 
for (ofi_vc_modal_ab_user_info *loc_obj in listData)
{
    if (strcasecmp(loc_obj->email, email) == 0) {
       // Set your flag here
       loc_found_obj = loc_obj;
        foundObject = YES;
        break;
    }
}
if(foundObject) {
// Do your stuffs as object is found
// Your found object is in loc_found_obj
 [listData removeObject:loc_found_obj];
}

I hope below code explains what you want. Please explain bit more if you need more help.

EDIT : If you are using NSMutableArray then you do not need index of the object. you can directly delete object as mentioned in my edited code.

Upvotes: 1

justin
justin

Reputation: 104708

you can just use C's for.

in fact, it's an error to mutate the collection you iterate over when using for (e in collection).

Upvotes: 5

Related Questions