【问题标题】:Get error when getting current date using NSDateComponents使用 NSDateComponents 获取当前日期时出错
【发布时间】:2015-05-11 01:58:43
【问题描述】:

我正在尝试使用NSDateComponents 获取当前日期。 (然后我将更改周数、月数等。)这是代码:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar component:(NSCalendarUnitCalendar |
                                                        NSCalendarUnitYear |
                                                        NSCalendarUnitMonth |
                                                        NSCalendarUnitDay) fromDate:[NSDate date]];

Xcode 给了我以下警告:

Incompatible integer to pointer conversion initializing 'NSDateComponents *__string' with an expression of type...'

还有错误:

Implicit conversion of 'NSInteger' (aka 'long') to 'NSDateComponents *' is disallowed with ARC

【问题讨论】:

    标签: ios objective-c nsdatecomponents


    【解决方案1】:

    您正在调用 component 方法(返回单个数字分量值),并且您打算调用 components(返回完整的 NSDateComponents 对象)。

    【讨论】:

      【解决方案2】:
      NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
      

      使用component 添加您要访问的 1 个单元。 对多个单元使用components

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 2013-07-31
        • 1970-01-01
        • 2012-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多