가능한 원형 참조 문제
-
06-07-2019 - |
문제
나는 바보가 아니지만 헤더 파일은 때때로 나를 느끼게합니다. 내가 해결할 수없는 오류가있는 과도하게 복잡한 설정이있을 수 있습니다. 여기에는 내가 만들 수있는 것만 큼 세부적인 것만 큼 간단합니다 ....
- 모델 클래스가 포함 된 컨트롤러 클래스가 있습니다.
- 동작을 캡처하고 컨트롤러와 의사 소통하는 장면 클래스가 있습니다.
- 모델 클래스와 대화하여 모델의 상태를 출력하는 레이어 클래스가 있습니다.
- 장면 클래스에는 출력을위한 레이어 클래스가 포함되어 있습니다.
- 장면은 CoCOS2D 프레임 워크에 의해 결정된대로 레이어를 유지해야합니다.
- 특정 장면 클래스는 컨트롤러 클래스에 대한 참조를 보유한 Rootscene 클래스에서 파생됩니다.
- 특정 레이어 클래스는 모델 클래스에 대한 참조를 보유한 루트 레이어 클래스에서 파생됩니다.
- 컨트롤러는 장면을 만드는 책임이 있으며 장면은 레이어를 작성하는 책임이 있습니다.
레이어를 만들고 컨트롤러 모델을 레이어 모델로 전달할 때 문제가 발생합니다. AScene.m
). 나는 "멤버 '모델 요청'을 혐의 나 노조가 아닌 것으로 얻습니다. 캐스팅은 작동하지 않으며이 수업이 서로 대화 할 수있는 방법을 잃어 버렸습니다. 문제의 일부는 컨트롤러 클래스에 모델 클래스가 포함되어 있다는 것입니다.
컨트롤러. H
#import <Foundation/Foundation.h>
@class Model;
@class AScene;
@interface Controller : NSObject {
Model *Model;
}
@property (nonatomic, retain) Model *Model;
-(void)runScene;
Controller.m
#import "Controller.h"
#import "Model.h"
#import "AScene.h"
@implementation Controller
@synthesize Model;
- (void)runScene {
AScene *newScene = [[AScene alloc] init];
newScene.controller = self;
}
Rootscene.h
#import "cocos2d.h"
@class Controller;
@interface RootScene : Scene {
Controller *controller;
}
@property (nonatomic, retain) Controller *controller;
@end
Rootscene.m
#import "RootScene.h"
#import "Controller.h"
@implementation RootScene
@synthesize controller;
- (id) init {
self = [super init];
if (self != nil) {
//
}
return self;
}
- (void) dealloc {
[controller release];
[super dealloc];
}
@end
Ascene.h
#import "RootScene.h"
@class ALayer;
@class Model;
@interface AScene : RootScene {
}
@end
Ascene.m
#import "AScene.h"
#import "ALayer.h"
#import "Model.h"
@implementation AScene
- (id) init {
self = [super init];
if (self != nil) {
ALayer *newLayer = [ALayer node];
newLayer.model = controller.Model; // <-- Request for member 'Model' in something not a stucture or union
[self addChild:statusScreenLayer];
}
return self;
}
- (void) dealloc {
[super dealloc];
}
@end
Rootlayer.h
#import "cocos2d.h"
@class Model;
@interface RootLayer : Layer {
Model *model;
}
@property (nonatomic, retain) Model *model;
@end
Rootlayer.m
#import "RootLayer.h"
#import "Model.h"
@implementation RootLayer
@synthesize model;
- (id) init {
self = [super init];
if (self != nil) {
//
}
return self;
}
- (void) dealloc {
[model release];
[super dealloc];
}
@end
Alayer.h
#import "RootLayer.h"
@interface ALayer : RootLayer {
}
-(void) draw;
@end
Alayer.m
#import "ALayer.h"
@implementation ALayer
-(void) draw {
// draw based on state of the model
}
@end
해결책
귀하의 구현 AScene
하지 않습니다 #import
헤더 Controller
.
편집하다: 명시 적 해결책.
Ascene.m 추가 :
#import "Controller.h"
다른 팁
니콜라이가 옳은 것 같습니다. 그 외에도 모범 사례는 속성 (예 : IVARS)이 대문자로 시작해서는 안된다고 지시합니다. 당신은 단지 상처를 요구합니다. 대신 이것을 사용하십시오.
컨트롤러. H
@interface Controller : NSObject {
Model *model;
}
@property (nonatomic, retain) Model *model;
Controller.m
@synthesize model;
제휴하지 않습니다 StackOverflow