Why methods dealloc and description in .m but not .h?


#1

Previously when we defined new methods to a class, we had to declare them in the header file.
In the subject example for the new asset class, we seemed to have skipped this and just defined the new methods in the implementation file.

???

thanks


#2

We don’t typically re-declare methods that are inherited (even if we override them). That is: you could declare dealloc and description if you wanted to, but they were declared in NSObject, so everyone knows you’ve got those methods.


#3

ok, that makes sense. thank you.
But now I’m fuzzy as to how these methods actually got executed since we never called them in main?

Here’s my stilted understanding:

  1. The dealloc method is automatically called by NSObject, so we don’t need to call it in main.
  2. Given that we are removing objects from the array that are in the employee (or asset) class, the over-ride dealloc methods take precendence, and this simply adds the NSLog method we added to employee.m (and asset.m) so we can see what’s taking place as NSObject executes dealloc behind the scenes.
  3. I have no idea how the description method was called.

I’m probably in so deep at the moment I’m missing an obvious fundamental, but this is all pretty new to me - thanks for your patience.


#4

It’s easy to get frustrated: any modern programming framework includes a great deal of implicit behavior, and figuring out what’s behind the curtain that you actually need to worry about (or, sometimes more importantly, what should be but isn’t behind the curtain) can be challenging.

description is invoked as part of the NSLog() invocation. Any time you see %@ appear in a format string, that means to take the corresponding object and invoke the description method; the resulting string will be incorporated into the log statement in that position.


#5

Thanks again to all on this forum for the tremendous amount of help offered with the utmost of courtesy and patience.
I can’t over emphasize how much of an asset this has been to augmenting the learning process; much credit to Aaron and the BNR staff for making this resource available.

Back to the issue above. I do recall seeing a section in the book that mentioned the @ token calling the ‘description’ method… here’s the excerpt (Kindle location 2394):

[color=#FF0000]- (NSString *)description
{ return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]]; }

  • (void)dealloc
    { NSLog(@“deallocating %@”, self); }

@end

Notice the %@ token in the format strings in the code above. This token is replaced with the result of sending the description message to the corresponding variable (which must be a pointer to an object so that it can receive this message).[/color]

I had probably read the above 5 times before I posted the above question on the ‘description’ method call and did not get it. I believe that I had understood it as you still needed to actually send (invoke) the description method to replace the token. I did not take it that the token itself would automatically invoke the description method.

I don’t know, maybe it’s just me; but take it for what it’s worth.

thanks again!