Pointer to Object Question


#1

In the challenge, in Portfolio.h and .m, when declaring and implementing the method that adds stocks to the portfolio, I don’t understand why the argument here goes to the Stockholding class. I don’t see anything in the Stockholding class that relates to this particular method. Also, if you had to parse this in English, how would it read (so I can better wrap my head around this). For example, does this mean, “declare a method called ‘addStockObject’ that uses an argument pointing to an instance of the Stockholding class.” How should it read?

  • (void)addStockObject:(StockHolding *)a;

And since it is using Stockholding, why doesn’t the class inherit from Stockholding instead of NSObject?

@interface Portfolio : NSObject

Also, I tried to use the debugger to trace the logic of the BMITime program but got lost as to when exactly ‘description’ and ‘valueOfAssets’ are called to print out the “<Employee 0: $0 in assets>”, “<Employee 1: $153 in assets>”, etc" list. It doesn’t seem like like these methods are explicitly called from Main, but do seem to be called when this NSLog (below) runs.

NSLog(@“Employees: %@”, employees);

Also, is there a way in XCODE to use the debugger to see when these are called?


#2

[quote=“standupguy”]In the challenge, in Portfolio.h and .m, when declaring and implementing the method that adds stocks to the portfolio, I don’t understand why the argument here goes to the Stockholding class. I don’t see anything in the Stockholding class that relates to this particular method. Also, if you had to parse this in English, how would it read (so I can better wrap my head around this). For example, does this mean, “declare a method called ‘addStockObject’ that uses an argument pointing to an instance of the Stockholding class.” How should it read?

  • (void)addStockObject:(StockHolding *)a;
    [/quote]

If I understand the question correctly, because this is declared in the interface for the Portfolio class, this method is tied to that class. Your English translation is correct, although I might tweak it slightly:

“addStockObject: is an instance method, returning no value, which takes one argument, an object of type StockHolding”

I addressed a similar question, breaking down the declaration of a method, in this thread: viewtopic.php?f=148&t=3357

Inheritance is best used sparingly. Objects frequently contain members of unrelated classes; for example, from the analog world, a vehicle has many parts, but is an instance of none of them. In this case, a portfolio contains stocks, but is not itself a stock. If you can’t say “A is an example of B”, then A should not inherit from B.

The converse is not always true: just because you can say “A is an example of B” does not mean that A should always inherit from B.

The NSObject base class provides a default description method that is invoked when NSLog is passed a format string that includes %@. You can experiment with that by declaring your own description method in Employee that returns a different string; you should see it in the console messages.

If you flag the NSLog code with a breakpoint, you should be able to step into that function, although I’m not sure what you’ll see. If you define your own description method, you should see it somewhere; alternatively, you can define description, set a breakpoint inside it, and then you’ll hit that breakpoint when NSLog() is invoked.

(Disclaimer: on the road, iPhone only, no reference book including this one handy, so there may be silly errors in the above.)