不兼容的指针类型将 'NSDate *' 发送到 'NSCalendar *' 类型的参数?
Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'?
先声明一下,这段代码不是我写的;它来自 tinyFool 于 2010 年编写的日历 API。现在,我正在尝试将其转换为推荐的 NSCalendar。如果您需要我没有提供的更多相关代码,请告诉我,我会 post 提供。
我有两个语句出现在几个地方:
[calendarViewDelegate monthChanged: currentMonthDate viewLeftTop:self.frame.origin height:height];
[calendarViewDelegate beforeMonthChange:self willto: currentMonthDate];
它们给我以下警告:
Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'
这是委托定义:
@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
代表是我的弱项之一;出于某种原因,我似乎无法理解它们,尽管我明白它们的目的是什么。当我查看 monthChanged 的代码时,我什么也没找到!所以我的问题是:如果它什么都不做,为什么作者要使用这段代码?以及如何更改代码以删除影响受影响方法正确运行的警告?
- (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height;
期望 NSCalendar
作为第一个参数,而您传递的是 NSDate
。您应该传递日历时传递日期,或者应该更新委托方法以采用日期参数而不是 NSCalendar 参数。那些委托方法是你写的吗?
如果 Self 是 NSCalendar
那么可能应该被称为
[calendarViewDelegate monthChanged:self viewLeftTop:self.frame.origin height:height];
否则如果你想传递一个 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
更新
如果说 MYView 需要在每次日期更改时显示警报,则委托中委托方法的示例实现
@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
先声明一下,这段代码不是我写的;它来自 tinyFool 于 2010 年编写的日历 API。现在,我正在尝试将其转换为推荐的 NSCalendar。如果您需要我没有提供的更多相关代码,请告诉我,我会 post 提供。
我有两个语句出现在几个地方:
[calendarViewDelegate monthChanged: currentMonthDate viewLeftTop:self.frame.origin height:height];
[calendarViewDelegate beforeMonthChange:self willto: currentMonthDate];
它们给我以下警告:
Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'
这是委托定义:
@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
代表是我的弱项之一;出于某种原因,我似乎无法理解它们,尽管我明白它们的目的是什么。当我查看 monthChanged 的代码时,我什么也没找到!所以我的问题是:如果它什么都不做,为什么作者要使用这段代码?以及如何更改代码以删除影响受影响方法正确运行的警告?
- (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height;
期望 NSCalendar
作为第一个参数,而您传递的是 NSDate
。您应该传递日历时传递日期,或者应该更新委托方法以采用日期参数而不是 NSCalendar 参数。那些委托方法是你写的吗?
如果 Self 是 NSCalendar
那么可能应该被称为
[calendarViewDelegate monthChanged:self viewLeftTop:self.frame.origin height:height];
否则如果你想传递一个 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
更新
如果说 MYView 需要在每次日期更改时显示警报,则委托中委托方法的示例实现
@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