【问题标题】:Determine iDevice current date in Xcode - NSDate在 Xcode 中确定 iDevice 当前日期 - NSDate
【发布时间】:2012-10-15 18:41:33
【问题描述】:

我想创建一个简单的 if 函数,它根据当前的 iDevice 年份是否为 2012 年执行一些代码。为此我尝试在 Xcode 中使用 NSCalendar 和 NSDateComponents 但失败了...... :(

任何人都知道有关于此类任务的教程/建议的任何好的资源或博客吗?

谢谢,

【问题讨论】:

    标签: ios xcode nsdate nscalendar nsdatecomponents


    【解决方案1】:
    NSDate *date = [NSDate date];
    NSCalendar *gregorian = [[[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    
    NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:date];
    int year = components.year;
    

    【讨论】:

    • 非常感谢。它就像一个魅力,我不得不对其进行一些编辑,因为我的 Xcode 项目启用了 ARC。但是非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多