سؤال

وأنا باستخدام رمز المقدمة أدناه لعرض الوقت والتاريخ. يمكن لأي شخص أن يساعدني في تغيير atuomatically الوقت بالثواني والتاريخ يوما بعد يوم؟

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];
هل كانت مفيدة؟

المحلول

ويمكنك استخدام NSTimer، وتحديدا <لأ href = "http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/ أورينت / هاء / NSTimer / scheduledTimerWithTimeInterval: الهدف: محدد: المعلومات حول المستخدم: يكرر: "يختلط =" نوفولو noreferrer "> scheduledTimerWithTimeInterval: الهدف: محدد: المعلومات حول المستخدم: يكرر: . يمكنك استخدام (1)، فترة زمنية.

نصائح أخرى

استخدم الطبقة NSDateComponents. على سبيل المثال، لإضافة يوم واحد وثانية واحدة إلى تاريخ:

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]; 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top