Question regarding addAssetsObjects: (Asset *) a method


#1

In the previous chapters an array was created by entering this line of code:

NSMutableArray *employee = [[NSMutableArray Alloc] init];

So how come we don’t have to include “NSMutableArray *” in front of “assets = [[NSMutableArray alloc]init ]”?


#2

Also,

I don’t understand the argument (Asset*) a. Why does the argument “a” have to belong to the Asset class? Wouldn’t the program still work properly if the argument was simply “a” and not (asset*) a? thanks in advance.


#3

That is because assets has already been declared as an instance variable of type NSMutableArray * in the Employee class.

If instead you had this:

- (void)addAssetsObject:(Asset *)a
{
    // Is assets nil?
    if (!assets)
    {
         NSMutableArray *assets = [[NSMutableArray alloc] init];
    }
    [assets addObject:a];
}

Then you would be creating inside the if-block a local assets object, and that object would not be visible outside the if-block (and could even leak memory), and then the statement [assets addObject:a]; down below would have no effect because assets would still be nil.

You need to revise your understanding of the distinction between locally declared variables and globally declared variables.

I don’t quite understand the question, but in this context if what you had in mind were this function definition:

- (void)addAssetsObject:(Asset)a
{
    ...
}

Or this definition:

- (void)addAssetsObject:a
{
    ...
}

They would not compile because they are syntactically wrong (The Objective-C language, like all other languages, has syntax rules that have to be religiously adhered to.)


#4

Hello everyone, this is my first post to this forum. First a little criticism on this chapter, it seems much harder to understand then the chapters that came before, perhaps it should be broken into 2. Secondly I would like to thanks BNR and Aaron Hillegass for this book and this forum, it is by far the best material that I have come across on this subject matter, and I have been searching quite a bit. Now to the first question I have:

Taken from ibex10’s response
"That is because assets has already been declared as NSMutableArray * in Employee class."

I can’t see where assets is declared as a NSMutableArray * in the Employee class.

Your elaborations are much appreciated, please go easy on me though because I’m very new to all of this.

Thanks kindly


#5

In my book: Chapter 19 - Page 120 - Top part. (2011, First Printing, November 2011)

Your book might be different though.


#6

Yes I finally found it, was having a blind moment, this chapter was very tricky, my eyes were flying all over the place.

I think Im going to have to start drawing diagrams in the future, because if this is considered easy, I would hate to see complex, or does it get better.

how do you deal with all the referencing thats going on? do you draw diagrams to make sense of it all?


#7

Diagrams can be very, very helpful. When I was learning, I spent a considerable amount of time diagramming my object hierarchies and relationships.

It’s no coincidence that when programmers talk about the relationships between all of the objects and pointers in an application, they refer to the big picture as the object graph.

Eventually, as you get more practice, you’ll get better at visualizing your program’s object graph in your head. Until then, though, draw away.