2009-07-01 44 views
55

我正在開始爲iPhone開發,因此我在網上查看不同的教程以及嘗試一些不同的東西。目前,我正在嘗試創建一個倒計時,直到午夜。要獲得的小時,分​​鍾和秒,我做了以下的數量(這是我發現的地方):每一個地方我用-dateWithCalendarFormat:timeZone:我如何獲得可可的當前日期

NSDate* now = [NSDate date]; 

int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay]; 
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour]; 
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute]; 
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec]; 

不過,我得到以下錯誤:

warning: 'NSDate' may not respond to '-dateWithCalendarFormat:timeZone:' 
(Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.) 
warning: no '-hourOfDay' method found 
error: invalid operands to binary - (have 'int' and 'id') 

這看起來很簡單。我錯過了什麼?

另外,我已經注意到在不同的地方和不同的時間星號(*)位於時間NSDate* now之後,或者位於變量NSDate *now之前。兩者有什麼區別,爲什麼你會使用一個與另一個?

回答

47

您必須使用以下方法:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:yourDateHere]; 
NSInteger hour = [dateComponents hour]; 
NSInteger minute = [dateComponents minute]; 
NSInteger second = [dateComponents second]; 
[gregorian release]; 

現在有現在的NSDate *和* NSDate的沒有什麼區別,它只是一個偏好的問題。從編譯器的角度看,沒有任何變化。

+0

你爲什麼要使用NSCalendar這是最近的更改?有很多代碼使用NSDate來處理這些事情。 – Jason 2009-07-01 17:40:04

+0

是的,許多方法現在已被棄用。 – 2009-07-01 17:51:00

+2

傑森:NSDate依然存在。您使用NSCalendarDate(這就是-dateWithCalendarFormat:時區:收益),並計劃棄用,並在iPhone上不存在。 – 2009-07-02 02:32:33

0

星號的位置沒有區別(在C中,Obj-C基於它,沒關係)。這是純粹的偏好(風格)。

2

NSDate* nowNSDate *now是一樣的東西:指向NSDate對象的指針。

你可能想使用descriptionWithCalendarFormat:時區:區域:而不是dateWithCalendarFormat: - 後者返回一個NSCalendarDate,該文件說計劃在某個時候被棄用。

3

你需要沿着以下的線路做一些事情:

NSDate *now = [NSDate date]; 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:now]; 
NSLog(@"%d", [components hour]); 

等。

1

這裏的另一種方式:

NSDate *now = [NSDate date]; 

//maybe not 100% approved, but it works in English. You could localize if necessary 
NSDate *midnight = [NSDate dateWithNaturalLanguageString:@"midnight tomorrow"]; 

//num of seconds between mid and now 
NSTimeInterval timeInt = [midnight timeIntervalSinceDate:now]; 
int hours = (int) timeInt/3600; 
int minutes = ((int) timeInt % 3600)/60; 
int seconds = (int) timeInt % 60; 

你輸了一秒的精度與NSTimeInterval的轉換爲int,但是這不應該的問題。

+0

第二次看,iPhone SDK可能沒有 - (NSDate)dateWithNaturalLanguageString:(NSString *)str方法,所以這可能是一個Leopard/Tiger唯一的解決方案。對不起 – 2009-07-01 19:40:53

21

您還可以使用:


CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); 
countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02.0f", currentDate.hour, currentDate.minute, currentDate.second]; 
CFRelease(currentDate); // Don't forget this! VERY important 

我認爲這具有以下優點:

  • 沒有直接的內存分配。
  • 秒是一個整數而不是整數。
  • 沒有留言通話。
  • 更快。
  • +1

    Opps忘記在你的stringWithFormat上加「@」。無論如何感謝這一點。 – andsien 2011-12-14 09:37:41

    +0

    你必須釋放來自CFTimeZoneCopySystem()返回的引用,否則你會得到一個內存泄漏。 – brutella 2012-07-31 07:53:40

    4

    Also, I've noticed at different places and at different times the asterisk (*) is located either right after the time NSDate* now or right before the variable NSDate *now . What is the difference in the two and why would you use one versus the other?

    編譯器不關心,而是把星號的空間之前,可能會產生誤導。這裏是我的例子:

    int* a, b; 
    

    b是什麼類型的?

    如果你猜int *,你錯了。這只是int

    另一種方式使這通過保持稍微更清晰的*旁邊的變量屬於:

    int *a, b; 
    

    當然,也有兩種方式都比更加清晰:

    int b, *a; 
    
    int *a; 
    int b; 
    
    +0

    是的,這是一個很好的參數:總是附加*到變量名;或從不在同一行上聲明兩個變量。就我個人而言,由於我更喜歡​​將「指向」作爲該類型的一部分,所以我選擇了後一種解決方案,但人們一定要意識到這個問題。 – 2009-07-02 03:15:05

    0

    我花了一段時間來找出爲什麼示例應用程序可以工作,但我的不知道。

    ,作者參考圖書館(Foundation.Framework)是系統庫(從OS),其中iPhone的SDK(我用3.0)不支持任何更多。

    因此示例應用程序(從about.com,http://www.appsamuck.com/day1.html)的作品,但我們沒有。

    +0

    Foundation仍然是iPhone SDK的一部分。你真的不能沒有它。缺少的是NSCalendarDate,它從來沒有在iPhone上可用(據我所知)。 – 2009-08-15 18:22:53

    5

    替換此:

    NSDate* now = [NSDate date]; 
    int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay]; 
    int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour]; 
    int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute]; 
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, min,sec]; 
    

    有了這個:

    NSDate* now = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now]; 
    NSInteger hour = [dateComponents hour]; 
    NSInteger minute = [dateComponents minute]; 
    NSInteger second = [dateComponents second]; 
    [gregorian release]; 
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minute, second]; 
    
    0
    CFGregorianDate currentDate = CFAbsoluteTimeGetGregorianDate(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); 
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%2.0f", currentDate.hour, currentDate.minute, currentDate.second]; 
    

    馬克是正確的驗證碼是更有效的管理日期小時分和秒。但是他在格式字符串聲明的開頭忘記了@。

    57

    您有iOS 4.2的問題? > 2011年1月20日10時36分02秒

    1

    原來的海報 - - 夏令時開始,當你確定秒鐘,直到午夜的方式不會在日常工作

    NSDate *currDate = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    [dateFormatter setDateFormat:@"dd.MM.YY HH:mm:ss"]; 
    NSString *dateString = [dateFormatter stringFromDate:currDate]; 
    NSLog(@"%@",dateString); 
    

    : 使用此代碼或結束。下面是一段代碼,它顯示瞭如何做到這一點...它將以秒爲單位(一個NSTimeInterval);你可以做分裂/模數/等等來滿足你需要的任何東西。

    NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]]; 
    [dc setDay:dc.day + 1]; 
    NSDate *midnightDate = [[NSCalendar currentCalendar] dateFromComponents:dc]; 
    NSLog(@"Now: %@, Tonight Midnight: %@, Hours until midnight: %.1f", [NSDate date], midnightDate, [midnightDate timeIntervalSinceDate:[NSDate date]]/60.0/60.0); 
    
    0
    + (NSString *)displayCurrentTimeWithAMPM 
    { 
        NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; 
        [outputFormatter setDateFormat:@"h:mm aa"]; 
    
        NSString *dateTime = [NSString stringWithFormat:@"%@",[outputFormatter stringFromDate:[NSDate date]]]; 
    
        return dateTime; 
    } 
    

    回報凌晨3:33

    12

    //你可以得到當前的日期/時間用下面的代碼:

    NSDate* date = [NSDate date]; 
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; 
        NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone]; 
        formatter.timeZone = destinationTimeZone; 
        [formatter setDateStyle:NSDateFormatterLongStyle]; 
        [formatter setDateFormat:@"MM/dd/yyyy hh:mma"]; 
        NSString* dateString = [formatter stringFromDate:date]; 
    
    相關問題