0

私はiPhoneアプリのObjective-cで最初の行を書いています。

これはコードです:

/* ViewController.h */
@protocol ImageFlowScrollViewDelegate;

@interface ViewController : UIViewController<ImageFlowScrollViewDelegate> {
    NSMutableArray *characters;
    UILabel *actorName;
}

/* ViewController.m */
#import "ImageFlowScrollView.h"
@implementation IMDBViewController
/* methods here */

/* ImageFlowScrollView.h */
@protocol ImageFlowScrollViewDelegate;

@interface ImageFlowScrollView : UIScrollView<UIScrollViewDelegate> {

    NSMutableArray *buttonsArray;
    id<ImageFlowScrollViewDelegate> imageFlowScrollViewDelegate;

}

@property(nonatomic, assign)id<ImageFlowScrollViewDelegate> imageFlowScrollViewDelegate;

- (id)initWithFrame:(CGRect)frame imageArray:(NSArray *) anArray;
- (void)focusImageAtIndex:(NSInteger) index;

@end


@protocol ImageFlowScrollViewDelegate<NSObject>

@optional
- (void)imageFlow:(ImageFlowScrollView *)sender didFocusObjectAtIndex: (NSInteger) index;
- (void)imageFlow:(ImageFlowScrollView *)sender didSelectObjectAtIndex: (NSInteger) index;
@end

これを行うと、私は

警告:プロトコル'ImageFlowScrollViewDelegate'の定義が見つかりません

私はそれを使用して修正することができます:

#import "ImageFlowScrollView.h"

@interface IMDBViewController : UIViewController<ImageFlowScrollViewDelegate> {
    NSMutableArray *characters;
    UILabel *actorName;
}

しかし、なぜ前方宣言アプローチが私に警告を与えているのか知りたいです。

4

1 に答える 1

1

前方宣言は、パーサーがそれを受け入れることができるようにシンボルを定義します。ただし、プロトコル(またはクラス)を使用しようとすると(プロトコルに準拠する場合と同様に)、コンパイラーは、結果のオブジェクトのレイアウトとサイズを知るための定義を必要とします。

さらに、クラスまたはプロトコルをクラスでのみ使用している場合(たとえば、ivar)に転送できます。その場合、コンパイラはシンボルの存在を知るだけで済みます。ただし、(実装ファイル内の)クラスを使用する場合は、使用する前にメソッドを宣言する必要があるため、宣言を含める必要があります。

例えば ​​:

/* AViewController.h */

@class AnotherClass;

@interface AViewController : UIViewController {
    AnotherClass* aClass; //only need the declaration of the name
}

@end

/* AViewController.m */

#import "AnotherClass.h"

@implementation AViewController

- (void) useAnotherClass {
     [AnotherClass aMessage]; //aMessage needs to be declared somewhere, hence the import
}

@end

さらに、プログラムをリンクするには、実際の実装を提供する必要があることをすでに知っています。

于 2010-08-27T14:58:18.870 に答える