不兼容的指针类型发送'类'到类型'NSDate *'的参数 [英] Incompatible pointer type sending 'Class' to parameter of type 'NSDate *'

查看:305
本文介绍了不兼容的指针类型发送'类'到类型'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 an NSString representation. Currently you are trying to convert the actual NSDate class into an NSString representation.

Change

+(NSString *)dateTimeStringForDB {

to

-(NSString *)dateTimeStringForDB {

这篇关于不兼容的指针类型发送'类'到类型'NSDate *'的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆