Please explain these 3 lines of code


#1
NSUInteger randomIndex = random() % [employees count];
Employee *randomEmployee = [employees objectAtIndex:randomIndex];
[randomEmployee addAssetsObjects:asset]; 

Except for the lines above, I think I have the code for the rest of this chapter figured out.

So this is what I understand so far:

randomIndex is an object of NSUInteger that gets assigned a random number from 0 - 9 from employees array.

*randomEmployee is a pointer to an object in the employees array that is assigned assigned by randomIndex. Why does it need to be an object pointer of Employee?

addAssetsObject method in randomEmployee is called and passed the parameter of asset (which is an object of Asset class). Why do we need to pass “asset” as a parameter?
Doesn’t addObjects only accept a pointer to an object of Asset?

I’m not sure what’s going on with these lines, especially the second and third line of code. Please advise.


#2

[quote]NSUInteger randomIndex = random() % [employees count];

randomIndex is an object of NSUInteger…[/quote]

I would call the randomIndex just an unsigned integer or a primitive object of type NSUInteger.

Because if you look up the definition of NSUInteger, you will see something like this (to do this: Option-click or Command-click the word NSUInteger in Xcode):

#if

typedef unsigned long NSUInteger;
#else

typedef unsigned int NSUInteger;
#endif

As shown above, NSUInteger is just a name for either unsigned long or unsigned int.

So NSUInteger is just a primitive data type in the Objective-C (or C) language. It is not a class; you can’t create instances of it by going for example: [[NSInteger alloc] init…].

[quote]NSUInteger randomIndex = random() % [employees count];

…that gets assigned a random number from 0 - 9 from employees array.[/quote]

There are two things here.

Firstly employees array contains employee objects; it does not contain numbers.

Secondly, assuming [employees count] > 0, the value of the expression “random() % [employees count]” is the remainder you get when you divide the random value by the number of elements in the employees array; so the value of randomIndex will always be >= 0 and < [employees count].

Therefore, better to say randomIndex is a primitive object of type NSUInteger that gets assigned a random value >= 0 and < [employees count].


#3

[quote]Employee *randomEmployee = [employees objectAtIndex:randomIndex];

randomEmployee is a pointer to an object in the employees array that is assigned assigned by randomIndex. … [/quote]

You probably meant “The value of randomEmployee is an object pointer at index ‘randomIndex’ in the employees array.”

[quote]Employee *randomEmployee = [employees objectAtIndex:randomIndex];

… Why does it need to be an object pointer of Employee? [/quote]

All Objective-C objects are accessed via pointers. Employees array contains pointers to Employee objects. When you index the array, you get a value which is a pointer to an Employee object, and the most natural place to store that value is in a variable of type Employee*.


#4

@ibrx10

Thank you for your reply.

I’m still struggling with this chapter. I have taken your advice and started reading “The C Programming Language, Brian W. Kernighan and Dennis M. Ritchie.”. I’m hoping that reading a C book will clearify some of the confusion. I’m sure you will hear back from me in these forums once I return to Chapter 19 after reading up on C.

Thanks for your help.