Multiple Mikey instances

I feel a bit stupid asking this many questions but just trying to get a grip on this chapter :frowning:

In main.m an employee array is filled with a FOR loop. And it’s filled by creating a new *mikey instance in every loop with different values for the variables.

If for example you try to do this sequentially like this:

BNREmployee *mikey = [[BNREmployee alloc]init];
mikey.employeeID = 1;
mikey.weightInKilos = 80;
        
BNREmployee *mikey = [[BNREmployee alloc]init];
mikey.employeeID = 2;
mikey.weightInKilos = 81;

you get an error on the second alloc/init line saying ‘Redefinition of mikey’.

why is that not happening in the loop?

Nobody? :frowning:

I wish I had an answer to this question. I never would’ve thought to ask it because I would expect a redefinition would automatically get rid of the old mikey with ARC.

I’d like somebody who knows for sure to confirm my guess, but I’m inclined to think it has to do with the line of code that follows…

            // put the employee in the employees array
            [employees addObject:mikey];

which takes the newly generated instance out of the for loop and stores it in the employees array.

That, or with the fact that the mikey instance is created within the for loop. trying to reference mikey from outside the {}s brings up an undeclared identifier error, while inside it works fine.

But really I have no idea, though I’d like to.

[quote]which takes the newly generated instance out of the for loop and stores it in the employees array.

That, or with the fact that the mikey instance is created within the for loop. trying to reference mikey from outside the {}s brings up an undeclared identifier error, while inside it works fine. [/quote]
That’s correct.

But to explain what’s going on, you need to invoke the concept of scopes.

A scope is an environment in which names exist as long as the scope itself exists.

A pair of braces signal the start and end of a scope.

{ // begin new scope
    Foo foo;
    Bar bar;
} // end scope

Scopes can be nested:

{ // begin outer scope
    Foo foo;
    Bar bar;
    ...
    { // begin inner scope
         // These are new names hiding the ones in the outer scope
         Foo foo;
         Bar bar;
         ...
    } // end inner scope
    ...
} // end outer scope

When a scope ends, all the names defined in the scope are gone. For example:

for (Foo * foo in foos)
{
    Bar bar;
    // Do something with foo and bar
    ...
}

Variable names foo and bar are defined within the scope of the for-loop and therefore do not exist outside the for-loop. If you want the same names, you must define them again:

for (Foo * foo in foos)
{
    Bar bar;
    // Do something with foo and bar
    ...
}
Foo * foo;
Bar bar;
// Do something with foo and bar
...

Ok, I get the scope part, reason why I didn’t think it was the solution is because the contents of the employees array is not gone after 1 for loop and in this array is already a mikey object.
Doesn’t that give a conflict when trying to put another one in?