我对 Objective-C 还很陌生,这是我在 StackOverflow 上的第一个问题。如果我遗漏了一些明显的东西,请提前原谅我。
我收到编译器警告
"Semantic Issue: Property type 'B *' is incompatible with type 'A *' inherited from 'X'"
代码仍然可以按预期编译和工作。我想知道我是否应该做一些不同的事情来删除编译器警告。
我有四个类定义如下:
//
// *** In class header file A.h
#import "X.h"
@interface A
@property X *variable1;
@end
//
// *** In class B header file B.h
#import "Y.h"
@interface B : A // B is a subclass of A
@property Y *variable1; // More specific type for property defined in superclass
@end
//
// *** In class X header file X.h
@class A; // Forward declaration to avoid circular imports
@interface X
@property A *variable2;
@end
//
// *** In class Y header file Y.h
@class B; // Forward declaration to avoid circular imports
@interface Y : X // Y is a subclass of X
@property B *variable2; // More specific type <-- I'm getting the compiler warning here
@end
我在 Y 类中 variable2 的属性声明中收到编译器警告。我已将此属性定义为类型 B,它是 A 的子类。此属性在超类中定义为类型 A。
如果我在类 Y 的标头而不是前向声明中 #import "Bh",编译器会看到 B 是 A 的子类并且警告消失。但是,我进入了#import 循环,因为 B 类的头文件也 #imports "Yh"。因此,我放入了前向声明以使代码编译,但我现在收到此编译器警告。
尽管有警告,代码仍然按我的预期工作。我想知道是否有适当的方法可以做到这一点,这样我就不会收到警告。非常感谢任何帮助或见解。谢谢!