Incompatible pointer type sending 'Class' to parameter of type 'NSDate *'

只愿长相守 提交于 2019-12-11 03:14:14

问题


I have an NSDate category with following method

@implementation NSDate (DateUtility)

+(NSString *)dateTimeStringForDB {
    NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
    [dateFormatForDB setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *aDateStr= [dateFormatForDB stringFromDate:self];
    [dateFormatForDB release];
    return aDateStr;    
}
@end

with this definition I receive a warning .

Incompatible pointer type sending 'Class' to parameter of type 'NSDate *'

However type casting self before assign it as argument suppresses this warning.

+(NSString *)dateTimeStringForDB
{
    NSDateFormatter *dateFormatForDB = [[NSDateFormatter alloc] init];
    [dateFormatForDB setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *aDateStr= [dateFormatForDB stringFromDate:(NSDate*)self];
    [dateFormatForDB release];
    return aDateStr;    
}

Can we really not pass self as an argument in a category without typecasting it ? What is this feature dependent on , the compiler ? Looking for an answer before actually posting it as a question on SO I came across this, however I am still not clear as to what goes behind the scene.


回答1:


You have created a class method, I suspect you really want an instance method. I assume you want to convert an instance of an NSDate (ie an object) into an NSString representation. Currently you are trying to convert the actual NSDate class into an NSString representation.

Change

+(NSString *)dateTimeStringForDB {

to

-(NSString *)dateTimeStringForDB {


来源:https://stackoverflow.com/questions/13081444/incompatible-pointer-type-sending-class-to-parameter-of-type-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!