Search code examples
objective-cdelegatesnsdatenscalendarios8.1

Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'?


Let me first say I didn't write this code; it was from the Calendar API written by tinyFool in 2010. As such, it did what it was supposed to do before Apple deprecated CFGregorianDate; now, I'm trying to convert it to the recommended NSCalendar. If you need more relevant code that I haven't supplied, let me know and I'll post it.

I have two statements that appear in several places:

    [calendarViewDelegate monthChanged: currentMonthDate viewLeftTop:self.frame.origin height:height];
    [calendarViewDelegate beforeMonthChange:self willto: currentMonthDate];

which are giving me the following warning:

Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'

This is the delegate definition:

@protocol CalendarViewDelegate <NSObject>
@optional
- (void) selectDateChanged:(NSCalendar *) selectDate;
- (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height;
- (void) beforeMonthChange:(CalendarView *) calendarView willto:(NSCalendar *) currentMonth;
@end

Delegates are one of my weak points; for some reason I can't seem to understand them, although I understand what their purpose is. When I look in my code for monthChanged, I find nothing! So my question is: why is the author using this code if it doesn't do anything? And how do I change the code to remove the warning, which is affecting the correct operation of the affected methods?


Solution

  • - (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height; expects an NSCalendar as the first arg and you are passing a NSDate. Either you are passing a date when you should be passing a calendar or the delegate method should be updated to take a date arg and not an NSCalendar argument. Did you write those delegate methods?

    If Self is a NSCalendar then probably should be called

    [calendarViewDelegate monthChanged:self viewLeftTop:self.frame.origin height:height];

    Else if you meant to pass an NSDate

    @protocol CalendarViewDelegate <NSObject>
    @optional
    - (void) selectDateChanged:(NSDate *) selectDate;
    - (void) monthChanged:(NSDate *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height;
    - (void) beforeMonthChange:(CalendarView *) calendarView willto:(NSDate *) currentMonth;
    @end
    

    UPDATE

    Example implementation of a delegate method in the delegate if say MYView needs to show an alert every time date changes

    @interface MYView ()<CalendarViewDelegate>
    @end
    @implementation MYView
    - (void)viewDidLoad{
        self.calendarView = [[CalendarView alloc] init];
        self.calendarView.delegate = self;
    }
    - (void) monthChanged:(NSDate *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"date changed" andBlahBlah];
        [alert show];
    }
    @end