Method Declaration


#1

Hello!

Inside the example of LotteryEntry.m class at the end was added

- (NSString *) description { NSString *result; result = [[NSString alloc] initWithFormat:@"%@ = %d and %d", [entryDate descriptionWithCalendarFormat:@"%b %d %Y"], firstNumber, secondNumber]; return result; }

My questions is:

  1. why this method has not been declared in the LotteryEntry.h file as other methods?
  2. If I understand correctly - NSLog call this method?

Thank you very much for assistance!


#2

[quote=“zhenyab”]Hello!

Inside the example of LotteryEntry.m class at the end was added

- (NSString *) description { NSString *result; result = [[NSString alloc] initWithFormat:@"%@ = %d and %d", [entryDate descriptionWithCalendarFormat:@"%b %d %Y"], firstNumber, secondNumber]; return result; }

My questions is:

  1. why this method has not been declared in the LotteryEntry.h file as other methods?
  2. If I understand correctly - NSLog call this method?

Thank you very much for assistance![/quote]

  1. You don’t need to because it is inherited from NSObject, so it’s already declared in NSObject.h (which is in turn imported by Foundation.h)
    see developer.apple.com/mac/library/ … ect_Class/

We’re overriding this method because (as you see in Figure 3.8 on page 51) NSObject’s description-method doesn’t know how to get something meaningful out of the LotteryEntry-class and only prints some pointer addresses (I suppose), you’ll read more about this in chapter 5

  1. Well… sort of :slight_smile: Page 38: “With NSLog, we have […] the token %@ to display an object. The object gets sent the message description, and the string it returns replaces %@ in the string.”

#3

Thanks a lot, whoami!

I start to understand :slight_smile: