1

I'm trying to get current hour minutes and seconds. Here is the code I use

NSDate *now = [NSDate date];

NSCalendar *gregorian = [[NSCalendar  alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];

[self.timerLabel setText:[NSString stringWithFormat:@"%@ %@ %@", [dateComponents hour], [dateComponents minute], [dateComponents second]]];

When I run the app crash on the line where self.timerLabel is.

What is wrong here ?

OhDoh
  • 433
  • 2
  • 9
  • 22

3 Answers3

1

When i run the code it works fine here.

what's the following line in your code ? Are you sure that it crashes on the third line in the code you pasted ? maybe the code you pasted is not the same than the one in your xcode.

Edit : it's because you're using %@ in your string which belongs to UTF-8 string. [dateComponents hour] etc. are returning NSInteger, so you should use %d instead.

moxy
  • 1,634
  • 1
  • 11
  • 19
0
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; 

NSInteger hour= [components hour];   
NSInteger minute = [components minute];
NSInteger second = [components second]; 
DJPlayer
  • 3,244
  • 2
  • 33
  • 40
0

Just doing

    NSDate *datenow = [NSDate date];

you will have the current date and time, to generate string representations of a date, you should use an instance of NSDateFormatter.

Evaristoyok
  • 244
  • 3
  • 14