【问题标题】:Property type '' is incompatible with type '' inherited from ''属性类型 '' 与继承自 '' 的类型 '' 不兼容
【发布时间】:2012-12-18 04:50:08
【问题描述】:

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

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

我想知道为什么。

这里有简化的演示:

BaseRequest.h

@interface BaseRequest

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

@end


@protocol BaseParams <NSObject>

- (NSMutableDictionary *)getParamsDict;

@end

图表请求.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

【问题讨论】:

    标签: objective-c compiler-warnings


    【解决方案1】:

    可以通过将DiagramParams 的接口定义放在DiagramRequest 之前来删除警告。

    在此处查看我的答案..https://stackoverflow.com/a/14632135/1347502 删除了警告,以实现稍微简单的设置。

    【讨论】:

      【解决方案2】:

      我对作为 UITextRange 子类的类 DTTextRange 有同样的问题,并且所有 UITextInput 方法都需要作为抽象类的 UITextRange。

      如果我有一个属性 selectedTextRange 将 DTTextRange 指定为类型,则会收到与您相同的警告,即 DTTextRange 与 UITextRange 不兼容。

      我能够消除该错误的唯一方法是在我的属性中也使用超类。

      简而言之,我希望能够像您一样做,如果您找到更改属性类型的方法,请告诉我们。

      如果不是,则必须删除子类属性并改用 id。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-08
        • 2018-05-29
        • 1970-01-01
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多