Jessica
Jessica

Reputation: 9830

Get error when getting current date using NSDateComponents

I'm trying to get the current date with NSDateComponents. (I will then be changing the weeks, months, etc.) Here is the code:

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

Xcode gives me the following warning:

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

And Error:

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

Upvotes: 2

Views: 578

Answers (2)

Anoop
Anoop

Reputation: 419

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]];

Use component to add 1 unit you want to access. Use components for more than one unit.

Upvotes: 1

Rob
Rob

Reputation: 438307

You are calling the component method (which returns a single numeric component value), and you meant to call components (which returns the full NSDateComponents object).

Upvotes: 3

Related Questions