不兼容的指针类型发送'类'到类型'NSDate *'的参数 [英] Incompatible pointer type sending 'Class' to parameter of type 'NSDate *'
问题描述
我有一个NSDate类别与以下方法
@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
p>
将Class类型发送给NSDate *类型的指针类型不兼容
但是,在分配它作为参数之前键入cast self会抑制此警告。
+(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;
}
我们真的不能把self作为参数传递到类中吗?这个功能是什么依赖的,编译器?在实际将其发布为一个问题之前寻找答案我遇到了这,但是我仍然不清楚
你创建了一个类方法,我怀疑你真的想要一个实例方法。我假设你想将一个 NSDate
(即一个对象)的实例转换为一个 NSString
表示。目前您尝试将实际的 NSDate
类转换为 NSString
表示。
更改
+(NSString *)dateTimeStringForDB {
/ pre>
到
- (NSString *)dateTimeStringForDB {
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.
解决方案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 anNSString
representation. Currently you are trying to convert the actualNSDate
class into anNSString
representation.Change
+(NSString *)dateTimeStringForDB {
to
-(NSString *)dateTimeStringForDB {
这篇关于不兼容的指针类型发送'类'到类型'NSDate *'的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!