根据我的理解,如果ClassA需要包含ClassB头,则应该使用前向声明,而ClassB需要包含ClassA头以避免任何循环包含.我也明白a #import是一个简单的,ifndef所以包含只发生一次.
我的询问是这样的:什么时候使用#import,什么时候使用@class?有时如果我使用@class声明,我会看到一个常见的编译器警告,如下所示:
warning: receiver 'FooController' is a forward class and corresponding @interface may not exist.
真的很想理解这一点,而不是仅仅删除@class前向声明并抛出一个#import静音编译器给我的警告.
我对一些我认为简单的事情有一个非常奇怪的错误.
#import <Foundation/Foundation.h>
#import "ViewController.h"
#import "GameObject.h"
@interface GameController : NSObject
@property (strong) GLKBaseEffect * effect;
@property (strong) NSMutableArray * gameObjects;
@property (strong) NSMutableArray * objectsToRemove;
@property (strong) NSMutableArray * objectsToAdd;
+ (GameController *) sharedGameController;
- (void) tick:(float)dt;
- (void) initializeGame: (ViewController*) viewcontroller;//ERROR: EXPECTED A TYPE
- (void) createObject:(Class) objecttype atPoint:(CGPoint)position;
- (void) deleteObject:(GameObject*) object atPoint:(CGPoint)position;
- (void) manageObjects;
@end
Run Code Online (Sandbox Code Playgroud)
为什么会问'ViewController'是否是一个类型?这是我正确实施的课程.它也被导入了.
编辑*
如果它有帮助,这是ViewController.m类.
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[GameController sharedGameController] initializeGame:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
编辑2 ** …
import objective-c circular-dependency header-files forward-declaration
所以我创建了这个方法:
- (void)addToViewController:(SecViewController *)controller
didFinishEnteringItem:(NSString *)item;` <br>
Run Code Online (Sandbox Code Playgroud)
并Xcode一直Expected a type在SecViewController*给我错误.那是什么意思?SecViewController不是一个类型?