concept description method in category ios

appears as: description method
Objective-C Fundamentals

This is an excerpt from Manning's book Objective-C Fundamentals.

The description method is declared in NSObject. By placing the same method in your class, you’re telling this class that rather than using the NSObject version of the description method, you want this method to be called. Now you override the description methods for both Teacher and Student, taking advantage of Person’s description method in the ones you create in Student and Teacher so that you can write as little code as possible:

- (NSString *)description {
    if(gender == Male) {
        return [NSString stringWithFormat:
                @"%@. I am a male currently teaching %@ "
                 "for $%@ per year with expertise in %@",
                 [super description], classes, salary, areaOfExpertise];
    } else {
        return [NSString stringWithFormat:
                @"%@. I am a female currently teaching %@ "
                 "for $%@ per year with expertise in %@",
                [super description], classes, salary, areaOfExpertise];
    }
}

Once again, you’ve overridden the description method so that when you use it in your NSLog method, it will be called rather than the NSObject version of it (see the following listing).

Listing 6.8. Overriding the description method in Student
- (NSString *)description { if(gender == Male) { return [NSString stringWithFormat: @"%@. I am a male currently enrolled in %@ " "for %@ credits with %@ as my major", [super description], classes, numberOfCredits, major]; } else { return [NSString stringWithFormat: @"%@. I am a female currently enrolled in %@ " "for %@ credits with %@ as my major", [super description], classes, numberOfCredits, major]; } }