可能的循环引用问题

发布于 2024-08-08 18:38:55 字数 3088 浏览 5 评论 0原文

我不是白痴,但头文件有时让我感觉自己像个白痴。我的设置可能过于复杂,并且存在无法解决的错误。在这里,它是我能做到的最简单的细节......

  • 我有一个包含模型类的控制器类。
  • 我有一个 Scene 类来捕获动作并与控制器进行通信。
  • 我有一个 Layer 类,它与 Model 类对话以输出模型的状态。
  • Scene 类包含仅用于输出的 Layer 类。
  • 场景必须包含由 Cocos2D 框架确定的图层。
  • 特定的 Scene 类派生自 RootScene 类,该类保存对 Controller 类的引用。
  • 特定的 Layer 类派生自 RootLayer 类,该类保存对 Model 类的引用。
  • 控制器负责创建场景,场景负责创建图层。

当创建一个图层并将控制器的模型传递给图层的模型(在 AScene.m 中)时,问题就出现了。我收到“请求在非结构或联盟中寻找成员‘模型’”。选角不起作用,我不知道如何让这些类相互交谈。我认为部分问题可能是控制器类包含模型类。

Controller.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

I am not an idiot, but header files make me feel like one sometimes. I have what is probably an overly-complicated set-up that has an error that I cannot resolve. Here it is in about as simple as detail as I can make it....

  • I have a Controller class that contains a Model class.
  • I have a Scene class to capture actions and communicates with the Controller.
  • I have a Layer class that talks with the Model class to output the state of the Model.
  • The Scene class contains Layer class solely for output.
  • Scenes must hold Layers as determined by the Cocos2D framework.
  • Specific Scene classes derive from a RootScene class that holds the reference to the Controller class.
  • Specific Layer classes derive from a RootLayer class that holds the reference to the Model class.
  • The Controller is responsible for creating Scenes and Scenes are responsible for creating Layers.

The problem comes when making a Layer and passing the Controller's Model to the Layer's Model (in AScene.m). I get the "Request for member 'Model' in something not a stucture or union". Casting doesn't work, and I'm at a loss for how to allow these classes to talk with each other. I think part of the problem might be that Controller class contains the Model class.

Controller.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心 2024-08-15 18:38:55

您的 AScene 实现不会 #import Controller 的标头。

编辑:明确的解决方案。

在AScene.m中添加:

#import "Controller.h"

Your implementation of AScene does not #import the header of Controller.

Edit: Explicit solution.

In AScene.m add:

#import "Controller.h"
ゝ杯具 2024-08-15 18:38:55

尼古拉似乎是对的。除此之外,最佳实践规定属性(如 ivars)不应以大写字母开头。你只是在自找伤害。请使用这些:

Controller.h

@interface Controller : NSObject {
    Model *model;
}
@property (nonatomic, retain) Model *model;

Controller.m

@synthesize model;

Nikolai seems to be right. Beyond that, best practice would dictate that properties (like ivars) should NOT start with an uppercase letter. You're just asking for hurt. Use these instead:

Controller.h

@interface Controller : NSObject {
    Model *model;
}
@property (nonatomic, retain) Model *model;

Controller.m

@synthesize model;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文