属性类型''与继承自''的类型''不兼容

时间:2012-12-18 04:50:08

标签: objective-c compiler-warnings

我创建了一个BaseRequest类,它有一个符合BaseParams协议的属性。 接下来我创建了一个继承自BaseRequest的DiagramReqesut类,它有一个属性类型为DiagramParam的属性,也符合BaseParams协议。

我认为编译是安全的,但xcode仍然警告: 属性类型'DiagramParams *'与从'BaseRequest'继承的类型'id'不兼容

我想知道为什么。

Simplified Demo就在这里:

BaseRequest.h

@interface BaseRequest

@property (nonatomic,retain) id<BaseParams> params;

@end


@protocol BaseParams <NSObject>

- (NSMutableDictionary *)getParamsDict;

@end

DiagramRequest.h

@interface DiagramRequest : BaseRequest

//warning: Property type 'DiagramParams *' is incompatible with type 'id<BaseParams>' inherited from 'BaseRequest'
@property (nonatomic,retain) DiagramParams *params;

@end


@interface DiagramParams : NSObject <BaseParams>

@property (nonatomic) int id;
@property (nonatomic,retain) NSString *city;

- (NSMutableDictionary *)getParamsDict;

@end

2 个答案:

答案 0 :(得分:3)

可以通过将第二个接口定义放在第一个接口之前来删除警告。

请在此处查看我的答案.. https://stackoverflow.com/a/14632135/1347502,它会删除警告,以便稍微简化设置。

答案 1 :(得分:0)

我对DTTextRange类有同样的问题,它是UITextRange的子类,所有UITextInput方法都需要UITextRange这是一个抽象类。

如果我有一个属性selectedTextRange指定DTTextRange作为类型获得与DTTextRange与UITextRange不兼容的相同警告。

我唯一可以解决这个问题的方法就是在我的属性中使用超类。

简而言之,我希望能够像你一样做,如果你找到改变房产类型的方法,请告诉我们。

如果没有,则必须删除子类属性并改为使用id。

相关问题