Cant figure this line of code


I went through this program with the debugger and understand what everything is doing except for this line. I don't understand how this line is putting the index number in employee id variable. *randomEmployee is just creating an instance and I dont see how this message send has anything to do with the employee id variable.

I went through this program with the debugger and understand what everything is doing except for this line. I don’t understand how this line is putting the index number in employee id variable. *randomEmployee is just creating an instance and I dont see how this message send has anything to do with the employee id variable.

That line is setting randomEmployee variable to point to the BNREmployee that is in the employees array at the index randomIndex. So if randomIndex were 0, you would get the first BNREmployee in the array, 1 the second, 2 the third, etc.

I had this same question, thanks for answering.

Regarding this and also the line of code that follows:

[randomEmployee addAsset:asset];

How does this all work in regard to the heap in memory? Do we now have 2 ‘Employee’ objects on the heap with one pointing at the other and each with a pointer on the stack pointing at it? Or do we just have 2 pointers on the stack both pointing to the one Employee object on the heap?

[quote=“alexalex2014”]Regarding this and also the line of code that follows:
[randomEmployee addAsset:asset];
[/quote]

Hi,

I’m a n00b but this is what I get from studying the book:

That line of code simply sends a message (addAsset) of an instance method, to the instance (randomEmployee) of BNREmployee with the argument (asset). The consequence of that is that you add an asset with its values to randomEmployee. You still only have one instance of BNREmployee, you are adding up to the array.

I hope this is clear (and correct).

best,

That is indeed correct, and clear. :slight_smile: