Aggiornamento in tempo SDK iPhone
-
21-08-2019 - |
Domanda
Sto usando il codice fornito di seguito per visualizzare l'ora e la data. qualcuno mi può aiutare con atuomatically cambiare il tempo da secondi e la data di giorno in giorno?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(@"\n"
"theDate: |%@| \n"
"theTime: |%@| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];
Soluzione
È possibile utilizzare NSTimer, in particolare scheduledTimerWithTimeInterval: target: selettore: UserInfo: ripete: . È possibile utilizzare 1 come intervallo di tempo.
Altri suggerimenti
Utilizzare la classe NSDateComponents
. Ad esempio, per aggiungere un giorno e un secondo per una data:
NSDate *startDate = [NSDate date];
unsigned unitFlags = NSDayCalendarUnit | NSSecondCalendarUnit;
NSCalendar *curr = [NSCalendar currentCalendar];
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
oneDay.day = 1;
oneDay.second = 1;
NSDate* adjustedDate = [curr dateByAddingComponents:oneDay
toDate:startDate
options:0];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow