12

나는 단순한 것으로 생각했을 때 매우 이상한 오류가 나타납니다.목표 C - 오류 : '예상 유형'

#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 

'ViewController'가 유형인지 여부에 대해 질문하는 이유는 무엇입니까? 그것은 올바르게 구현 한 클래스입니다. 그것은 또한 수입되었습니다. 도움이된다면 편집 *

다음은 ViewController.m 클래스입니다.

#import "ViewController.h" 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[GameController sharedGameController] initializeGame:self]; 
} 

@end 
편집 2 **

과 ViewController.h

#import <GLKit/GLKit.h> 
#import "GameController.h" 

@interface ViewController : GLKViewController 

@end 
+2

어떻게'ViewController'가 정의 되었습니까? – Wain

+0

이것으로 도움이 될 것입니다. 비슷한 질문 : http://stackoverflow.com/a/9607607/1422070 – edwardmp

+0

.h 파일 내부에 클래스 이름의 철자가 잘못 표시되었을 수 있습니다. –

답변

34

사용에게 앞으로 선언을 파일 : @class ViewController;#import "ViewController.h" 대신에. 가져 오기는 Objective-C의 다른 헤더에서 일반적으로 불필요합니다.

GameControllerViewController을 사용하는 경우 가져 오기를 GameController.m에 추가 할 수 있습니다.

순환 의존성이있을 수 있습니다.

순환 종속성을 정의하는 기본적인 방법은 다음

  • GameController.h 수입 ViewController.h
  • ViewController.h 수입 GameController.h 하나가 먼저 나타나는에서 선언의 순서에 의존한다

번역이 가능하지만 분명히이 경우에는 머리글이 어느 쪽이 먼저 와야하는지에 대해 동의하지 않기 때문에 먼저 오는 것이 좋습니다.

실제 코드베이스에서는 많은 소스 파일에 #import "ViewController.h"이 표시 될 수 있습니다. 이것은 매우 복잡한 종속성을 만듭니다. 이러한 종속성은 ObjC에서 대부분 필요하지 않습니다. 헤더에서 대부분의 시간에 순방향 선언을 사용할 수 있으며 빌드 시간이 향상됩니다.

그래서 가장 단순한 경우를 설명했지만 15 개의 헤더가있을 때 어떻게됩니까 #import ViewController.h? 음, 어떤 헤더가 해당 번역에 대한 순환 종속성을 도입하는지 추적해야합니다. 종속성을 잘 관리하지 않으면 수십 (또는 수백) 개의 파일을 단계별로 분석해야 할 수 있습니다. 때로는 해당 번역에 대한 사전 처리 된 출력 (예 : *.m 파일)을 검토하는 것이 가장 쉽습니다. 종속성이 최소화되지 않으면 그 출력은 수십만 줄에 달할 수 있습니다 (올바르게 관리되는 경우 빌드 시간은 20 배 이상 빨라질 수 있습니다). 따라서 순환 의존성을 찾는 복잡성은 대규모 코드베이스에서 빠르게 발생합니다. 헤더에 #import#include이 더 많이 있습니다. 가능한 경우 헤더에서 forward 선언을 사용하면이 문제를 해결할 수 있습니다.

#import "GameController.h" 
#import "ViewController.h" // << if you need it in this source 
#import "GameObject.h" // << if you need it in this source 

@implementation GameController 
... 

그런 GameController.m

// includes 
#import <Foundation/Foundation.h> 

// forwards required by this header  
@class GameObject; 
@class GLKBaseEffect; 
@class ViewController; 

// header declarations 
@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 

GameController.h :

그래서 영업 이익 헤더 주어, 당신은 그것을 다시 작성할 수 같은 치료법을 적용 할 수 있습니다. ViewController.h (GameController.h 가져 오기).

+0

전달 선언이 포함되어 있으며 헤더를 '.m'파일로 가져 왔습니다. 나는 여전히 같은 오류가 발생합니다. '순환 의존성'문제를 좀 더 설명 하시겠습니까? – user2577959

+0

프로젝트에 순환 종속성이 있습니다. 어떻게 수정해야합니까? – user2577959

+0

@ user2577959가 예 – justin

0

ViewController.h 내에 정의 된 클래스 인터페이스의 철자가 "ViewController"이니 확실합니까?

내 프로젝트에서 ViewController.h 클래스를 작성하여 빠른 테스트를 수행했지만 인터페이스 이름을 ViewControllers으로 변경하여 동일한 오류가 발생했습니다.

+0

그냥 두 번 확인했습니다. 모든 것이 바르게되어 있습니다. – user2577959

+0

ViewController에 대한 선언을 게시 할 수 있습니까? – Sid