My class representing a person has a method to calculate that person's age:
@interface Student : NSObject
@property (strong) NSDate *dob;
// This method will work out the person's age in calendar years
- (NSDateComponents*)ageCalculation:(NSDate*)dob;
Here is the class implementation file
@implementation Student
- (NSDateComponents*)ageCalculation:(NSDate*)dob {
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *components = [calendar components:units
fromDate:dob
toDate:now
options:0];
return components;
}
@end
I'm not sure that I'm doing this right, though:
Student *student1 = [[Student alloc] init];
[student1 setDob:aDateOfBirth];
NSDateComponents *ageComponents = [student1 ageCalculation:aDateOfBirth];
What should I do with the result of this method? How can I make ageCalculation:
use the date I already set?