1

Need to convert this to Objective-C syntax, can't seem to find a way to convert it.

   private class func components(fromDate fromDate: NSDate) -> NSDateComponents! {
        return NSCalendar.currentCalendar().components(NSDate.componentFlags(), fromDate: fromDate)
    }

Tried this:

- (NSDateComponents *) componentsFromDate: (NSDate *) fromDate {
    return [[NSCalendar currentCalendar] componentsFromDate: [NSDate componentFlags] : fromDate];
}

This was defined before:

- (NSCalendarUnit *) componentFlags {
    return NSCalendarUnitYear, NSCalendarUnitMonth, NSCalendarUnitDay, NSCalendarUnitWeekOfYear, NSCalendarUnitHour, NSCalendarUnitMinute, NSCalendarUnitSecond, NSCalendarUnitWeekday, NSCalendarUnitWeekdayOrdinal, NSCalendarUnitWeekOfYear;
}

All this in a category I created:

@implementation NSDate (AFDateHelperObjC)
@end

1 Answers1

0

You need:

- (NSDateComponents *) componentsFromDate: (NSDate *) fromDate {
    return [[NSCalendar currentCalendar] components:[NSDate componentFlags] fromDate:fromDate];
}

The name of the method in NSCalendar is components:fromDate:.

And your componentsFlags method that you added to NSDate as a category method needs to be a class method, not an instance method. In both the .h and .m of the category, change the - to +. And the return type needs to be NSCalendarUnit, not NSCalendarUnit *. And you need to use bitwise OR to join all of the flags together.

+ (NSCalendarUnit) componentFlags {
    return NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitWeekOfYear;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Tells me : no known class method for selector: "componentFlags" –  Apr 11 '16 at 16:26
  • 1
    Oh yeah. That makes sense. Where did you define that `componentsFlags` method you added to your question? If it was added to `NSDate` as a category method, it should be a class method, not an instance method (change the `-` to a `+`). – rmaddy Apr 11 '16 at 16:27
  • 1 last minor remark from XCode: - expression result unused for the componentFlags method. –  Apr 11 '16 at 16:31
  • One more edit. There were lots of issues wit the `componentFlags` method that I overlooked initially. – rmaddy Apr 11 '16 at 16:37
  • Thx ! Answer confirmed ! –  Apr 11 '16 at 17:17