Question on NSLog, stringByAppendingFormat and using self


#1

While I was trying to do the Gold Challenge, I encountered some questions that hopefully someone here could further elaborate. All my questions are pretty much discovered when I rewrote the description method for BNRContainer in BNRContainer.m. (See below)

[code]- (NSMutableString *)description
{
[self calculateTotalValueInDollars];
NSMutableString *descriptionString = [[NSMutableString alloc] init];
[descriptionString appendFormat: @"\nName of container : %@", [self containerName]];
[descriptionString appendFormat:@"\nTotal value : $%d", [self totalValueInDollars]];
[descriptionString appendFormat:@"\nItems in container :\n%@", [self container]];

return descriptionString;

}[/code]

  1. When I was debugging the description method, I wanted to use NSLog inside the description method but it did not work. For example. I tried NSLog(@"%d", [self totalValueInDollars]); but it did not print anything. My question is why did it not print ?

In the next chapter about memory management, I added the method

- (void)dealloc { NSLog(@"Destroyed: %@", self); } to BNRItem.m which also did not print out anything.

  1. Before I ponder on appendFormat, which worked for me. I was trying to use stringByAppendingFormat, which did not work. I just wanted to append to descriptionString. Docs says “Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments”, which seems right but did not work. Could someone explain why ?

  2. I realise the book did not use self when rewriting the description method in BNRItem. Both return the same answer. My question is what is the difference ? I entered gdb and realize that without self, it seems that variables like container or totalValueInDollars are observed as local variables. On the other hand, if self is used, gdb does not create extra local variables. Seems like having self is better but I am not sure. Hopefully someone could clear my doubts here.

Appreciate any help. Thanks!