So I am busy reading an objective-c book by Big Nerd Ranch. I'm on chapter 17 at the moment and managed to complete the required challenge at the end of the chapter. However, I just have two question that I would like to understand.

In the following bit of code - StockHolding is a custom class that has instance variables and the stocks (an array) points to three instances of stockholding with values setting its stock value and cost in dollars.

At first I tried to access the array to get the data from the objects it pointed to - but it seems that was not going to work as the array doesn't know what data its objects contain - just where they are in memory, right?

What I want to know is why was it necessary to create a new instance of stockholding (holdings) in this for loop to access those variables?
How does the new instance of stockholding know what the values of my stocks are?

You are confusing instances with variable names. holdings is not an instance. It points to an instance. A pre-existing instance. Not a new one. Each iteration through the loop the pointer is updated to point at the next instance from the collection stocks.

What you have there is called Fast Enumeration. It's a way of iterating through an array really fast. You are not creating a new instance of StockHolding you are merely accessing an existing instance from the array.

What you have there is called Fast Enumeration. It's a way of iterating through an array really fast. You are not creating a new instance of StockHolding you are merely accessing an existing instance from the array.

Cool: I know in Java tests have shown that using the Iterator is slower than a straight for loop. I would be interested in seeing the results if you saved the count into a variable instead of checking it every iteration of the for loop...

You are confusing instances with variable names. holdings is not an instance. It points to an instance. A pre-existing instance. Not a new one. Each iteration through the loop the pointer is updated to point at the next instance from the collection stocks.

Click to expand...

Okay so we are point at the instance of stockholding using the pointer *holdings. That clears it up a little.

When you say pre-existing instance - is that stockholding you're referring it?

What you have there is called Fast Enumeration. It's a way of iterating through an array really fast. You are not creating a new instance of StockHolding you are merely accessing an existing instance from the array.

These two loops provide the same functionality but the former one is much faster. If you look at the second loop, you are just accessing the object, not creating a new instance.

Click to expand...

Thanks - the link does help. So what I get from that is we point to each object in the array, using the instance stockholding and we call this instance holdings. Which is why [holdings currentSharePrice] works in the NSLog function, right?

Since currentSharePrice is an instance variable inside our stockholding class - so we can now access it using an instance of the class?

stocks contains a number of instances of StockHolding. Instances don't have names: it makes no sense to ask if an instance is "stockholding" or any other name. Instances do, however, have memory addresses.

If we step aware from Objective-C for a bit and pretend that stocks is a plain old array (not a NSArray which I assume it is). An array is a contiguous block of memory starting at an address like this:

Code:

Memory: 0000 0001 .... 0100
Contents: [ ] [ ] [ ]

If this is an integer array then the contents could be numbers:

Code:

Memory: 0000 0001 .... 0100
Contents: [ 1] [ 5] [-10]

So far, so simple. But what if our array contains pointers:

Code:

Memory: 0000 0001 .... 0100
Contents: [1001] [1100] [1111]

So now the first element of our array does not contain a value: it contains a pointer to a value. This is the situation we have in this example. An instance of any class is simply a structure in memory. This structure is, in reality, quite complicated but we can simplify it to something like this:

where isa is a pointer to the class and the rest are the instance variables. These will either be the raw value in the case of primative types or a pointer to another object in the case of object types.

When we create the array stocks what is actually happening is something like this:

So we have an array which contains 3 pointers. These pointers point to 3 instances of StockHolding (3 memory addresses). Each of these objects has a single instance variable, the amount of the holding for this instance (100, 400 and -200). As we loop over the array holdings points first to 0010000 then the second time 0010010 and the third time 0010100. So it points to the existing instances, not new ones. When we as for the holding amount we get the amount from the instance we are pointing at: instance variables are at an instance level, not the class level:

Code:

StockHolding a = [[StockHolding alloc] init];
[a setSharePrice:100];
StockHolding b = a; // so now b and a point to the same instance
[b setSharePrice:200];
NSLog(@"%i",[a getSharePrice]);

200 will be printed as both a and b point to the same instance: they point to the same object in memory.

stocks contains a number of instances of StockHolding. Instances don't have names: it makes no sense to ask if an instance is "stockholding" or any other name. Instances do, however, have memory addresses.

If we step aware from Objective-C for a bit and pretend that stocks is a plain old array (not a NSArray which I assume it is). An array is a contiguous block of memory starting at an address like this:

Code:

Memory: 0000 0001 .... 0100
Contents: [ ] [ ] [ ]

If this is an integer array then the contents could be numbers:

Code:

Memory: 0000 0001 .... 0100
Contents: [ 1] [ 5] [-10]

So far, so simple. But what if our array contains pointers:

Code:

Memory: 0000 0001 .... 0100
Contents: [1001] [1100] [1111]

So now the first element of our array does not contain a value: it contains a pointer to a value. This is the situation we have in this example. An instance of any class is simply a structure in memory. This structure is, in reality, quite complicated but we can simplify it to something like this:

where isa is a pointer to the class and the rest are the instance variables. These will either be the raw value in the case of primative types or a pointer to another object in the case of object types.

When we create the array stocks what is actually happening is something like this:

So we have an array which contains 3 pointers. These pointers point to 3 instances of StockHolding (3 memory addresses). Each of these objects has a single instance variable, the amount of the holding for this instance (100, 400 and -200). As we loop over the array holdings points first to 0010000 then the second time 0010010 and the third time 0010100. So it points to the existing instances, not new ones. When we as for the holding amount we get the amount from the instance we are pointing at: instance variables are at an instance level, not the class level:

Code:

StockHolding a = [[StockHolding alloc] init];
[a setSharePrice:100];
StockHolding b = a; // so now b and a point to the same instance
[b setSharePrice:200];
NSLog(@"%i",[a getSharePrice]);

200 will be printed as both a and b point to the same instance: they point to the same object in memory.

Click to expand...

Okay that is making a lot more sense now.

I really appreciate the time you have taken in explaining this to me.
Doing this on my own with no one around who knows objective-c is difficult as I have to do a lot of the research on my own.

I am going to re-read your explanation again tomorrow as I am tired today so I don't think I am "absorbing" as much as I could be at this point in time.

I am going to re-read your explanation again tomorrow as I am tired today so I don't think I am "absorbing" as much as I could be at this point in time.

Click to expand...

This is probably a good idea. Sometimes stepping away and coming back makes everything just snap into place. The one thing I would suggest is learning plain old C. Pointers, memory etc are all based on that and a good understanding of that could help here.

This is probably a good idea. Sometimes stepping away and coming back makes everything just snap into place. The one thing I would suggest is learning plain old C. Pointers, memory etc are all based on that and a good understanding of that could help here.

As a total beginner would I struggle with this book?
Also can I use xCode to compile / run the examples / exercises in the book on?

Click to expand...

It is the book written by the guys who designed an implemented C. As such it has everything. The book recommended when I was at Uni was "A Book on C" by Kelly and Pohl. It was good enough to learn from

It is the book written by the guys who designed an implemented C. As such it has everything. The book recommended when I was at Uni was "A Book on C" by Kelly and Pohl. It was good enough to learn from

Click to expand...

Having done a little C (From current book ) - maybe I should just read THE book of C and see how it goes?

If I find it difficult maybe I will get another book with a different learning style.

As a total beginner would I struggle with this book?
Also can I use xCode to compile / run the examples / exercises in the book on?

Click to expand...

I've read both the K&R book and the "A book on C" book by Kelly and Pohl. The K&R book is better written.

K&R is very clear and well written. It requires work from you in order to learn from it. If you read the entire teaching section of the book and do all the exercises, you will have a good working knowledge of C (including pointers) when you are done.

I've read both the K&R book and the "A book on C" book by Kelly and Pohl. The K&R book is better written.

K&R is very clear and well written. It requires work from you in order to learn from it. If you read the entire teaching section of the book and do all the exercises, you will have a good working knowledge of C (including pointers) when you are done.

Click to expand...

Thanks Duncan. Would you say it's better to put down the Objective-C book and pickup the K&R book and finish it. Then carry on with the OC book?
Or side by side?

Was planning on reading both on a per chapter basics. So read chapter one of the K&R book. Then read the current chapter that I am on with the second book - rinse and repeat?

Thanks Duncan. Would you say it's better to put down the Objective-C book and pickup the K&R book and finish it. Then carry on with the OC book?
Or side by side?

Was planning on reading both on a per chapter basics. So read chapter one of the K&R book. Then read the current chapter that I am on with the second book - rinse and repeat?

Click to expand...

If you were reading the Big Nerd Ranch Objective C book I would say you could just read that book, as the first several chapters teach you a crash course in C.

I'm not as familiar with the other book even though I own it. (I teach classes on this stuff sometimes so I have quite a few books at different levels.)

I would not suggest reading a chapter of K&R, then a chapter of Objective C, then another chapter of K&R. That's going to leave you very confused.

If you're going to study C, read the entire K&R teaching section and do all the exercises, including creating linked lists, etc. If you do that, you will have not only a solid grounding in C, but a solid grounding in computer science. If I remember correctly, it goes through exercises in Linked Lists, binary trees, stacks, etc. Great fundamentals that many people starting out never get.

Expect it to hurt your head more than a little. Remember that by reading all of K&R you're covering a lot of the fundamentals of computer science, written by the guys who created UNIX and C.

If you were reading the Big Nerd Ranch Objective C book I would say you could just read that book, as the first several chapters teach you a crash course in C.

I'm not as familiar with the other book even though I own it. (I teach classes on this stuff sometimes so I have quite a few books at different levels.)

I would not suggest reading a chapter of K&R, then a chapter of Objective C, then another chapter of K&R. That's going to leave you very confused.

If you're going to study C, read the entire K&R teaching section and do all the exercises, including creating linked lists, etc. If you do that, you will have not only a solid grounding in C, but a solid grounding in computer science. If I remember correctly, it goes through exercises in Linked Lists, binary trees, stacks, etc. Great fundamentals that many people starting out never get.

Expect it to hurt your head more than a little. Remember that by reading all of K&R you're covering a lot of the fundamentals of computer science, written by the guys who created UNIX and C.

Click to expand...

Thanks Duncan.

I'll finish the BNR book first and will start K&R book as weekend project (IE read it every weekend) as I cannot read it while at work as I have some deadlines to meet.

With regards to that K&R book - is it possible to follow it properly on my MacBook using xCode - or will I need a different compiler?

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.