OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 NSDateComponents 获取当前日期时出错 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:45
标题: ios - 使用 NSDateComponents 获取当前日期时出错

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

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar componentNSCalendarUnitCalendar |
                                                        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



Best Answer-推荐答案


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

关于ios - 使用 NSDateComponents 获取当前日期时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158627/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4