1

I have been learning Objective-C from Big Nerd Ranch Book. I am stuck on 21st chapter example code.

Here is class BNREmployee header and Main file code.

Header

@interface BNREmployee : BNRPerson
{
    NSMutableArray *_assets;
}

@property (nonatomic) unsigned int employeeID;
@property (nonatomic) unsigned int officeAlarmCode;
@property (nonatomic) NSDate *hireDate;
@property (nonatomic, copy) NSArray *assets;
- (double)yearsOfEmployment;
- (void)addAsset:(BNRAsset *)a;
- (unsigned int)valueOfAssets;

@end

Main.m

for (int i = 0; i < 10; i++) {
            // Create an instance of BNREmployee
            BNREmployee *mikey = [[BNREmployee alloc] init];

            // Give the instance variables interesting values
            mikey.weightInKilos = 90 + i;
            mikey.heightInMeters = 1.8 - i/10.0;
            mikey.employeeID = i;

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

So, my question is how are we creating multiple objects for BNREmployee class with same name i.e., 'mikey'. It is done inside the loop. But, how does it work, why the compiler is allowing that? Thanks

5
  • 1
    Objects don't have names (unless they contain, eg, a name property). Pointers have names. Entirely different things (and a fundamental point you should understand BEFORE trying to code in Objectiver-C). Commented Dec 1, 2014 at 12:48
  • Objects and pointers equally don't have names. Variables have names. Commented Dec 1, 2014 at 12:48
  • @tenfour - Point taken! Commented Dec 1, 2014 at 12:49
  • There are no names in assembly, only addresses Commented Dec 1, 2014 at 22:38
  • en.wikipedia.org/wiki/Scope_%28computer_science%29#Block_scope Commented Dec 2, 2014 at 7:55

1 Answer 1

5

This is a very basic syntax question about objective-C. mikey is a variable that points to an instance of a BNREmployee. Each loop iteration, you create a new BNREmployee instance, and overwrite mikey to point to the new instance.

This line:

BNREmployee *mikey = [[BNREmployee alloc] init];

Creates an instance of BNREmployee, and assigns it to mikey. You can create another instance of BNREmployee and do the same again:

mikey = [[BNREmployee alloc] init];// same thing again.

Doing this in a loop is basically the same as doing this. A simpler example is:

int mikey = 1;
mikey = 2;
mikey = 3;

This is the same concept; you're creating integers and assigning them to mikey.

In your code, the instance is stored in employees via the line [employees addObject:mikey];. Now the BNREmployee instance you created is stored in multiple places: once in mikey, and once somewhere in employees. When the loop continues, mikey gets reassigned to another new instance. employees continues to accumulate the instances you created.

Why do you think the compiler shouldn't allow this?

Sign up to request clarification or add additional context in comments.

2 Comments

In one line "You are overwriting mikey every iteration".
Eloquence never was my strength :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.