5

I have a hypothetical UIViewController class named "foo". foo inherits from class bar and class bar #import's "Class A", a class which foo uses extensively. The problem is, when I'm using an instance of class A in foo, I don't get any compiler errors, but I do get a warning for instance, that an instance of Class A does not respond to a particular method. Do I have to explicitly #import ClassA.h into class 'foo'? even though class foo extends extends bar, which already imports it?

Hope that's not too confusing. Let me know if I need to clear anything up.

4

1 に答える 1

12

循環依存の問題があるようです。それを解決するには、はい、各実装ファイル ( .m)#importに適切なヘッダー ファイルが必要です。ただし、ヘッダーファイルを#import相互に保持しようとすると、問題が発生します。

継承を使用するには、スーパークラスのサイズを知る必要があります。つまり、それが必要です#import。ただし、ポインターであるメンバー変数や、パラメーターとして受け取るメソッドや他の型を返すメソッドなど、他のものについては、実際にはクラス定義は必要ないため、前方参照を使用してコンパイラ エラーを解決できます。

// bar.h
@class A;  // forward declaration of class A -- do not to #import it here

@interface bar : UIViewController
{
    A *member;  // ok
}

- (A) method:(A)parameter;  // also ok
@end

// bar.m
#import "bar.h"
#import "A.h"

// can now use bar & A without any errors or warnings
于 2009-02-22T16:35:04.237 に答える