Getting MAX_INT from NSDateComponents

I am trying to write my first iPhone app, and I am using a date picker so that the user can enter their date of birth. Part of my application requires a year in int format. I have added the code as shown below. What is strange is that the "month" gets the correct value. But the day and year seem to be stuck in MAX_INT (2147483647). Can someone tell me what I am doing wrong?

-(IBAction)dateOfBirthChanged:(id)sender { NSCalendar* calender = [NSCalendar currentCalendar]; NSDateComponents* dateComponents = [calender components:NSMonthCalendarUnit fromDate:[datepicker date]]; NSInteger day = [dateComponents day]; NSInteger month = [dateComponents month]; NSInteger year = [dateComponents year]; label.text = [NSString stringWithFormat:@"Your year of birth is %d", year]; } 
+4
source share
1 answer

Try this instead:

 NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[datepicker date]]; 

Link: - components: fromDate:

+6
source

Source: https://habr.com/ru/post/1305900/


All Articles