可能的循环参考问题

编程入门 行业动态 更新时间:2024-10-24 18:21:32
本文介绍了可能的循环参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不是一个白痴,但头文件让我感觉像一个有时。我有什么可能是一个过于复杂的设置,有一个错误,我不能解决。

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

  • 我有一个Controller类,它包含一个Model类。
  • 我有一个Scene类来捕获动作并与Controller通信。
  • 我有一个Layer类,
  • Scene类包含仅用于输出的Layer类。
  • 场景必须包含由Cocos2D框架确定的图层。 li>
  • 特定的Scene类派生自一个保存对Controller类的引用的RootScene类。
  • 特定的Layer类派生自一个RootLayer类,
  • 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.

问题出在制作图层并将控制器的模型传递给图层的模型(在 AScene.m 中)。我得到请求成员'模型'在不结构或联合的东西。铸造不工作,我不知道如何允许这些类彼此交谈。我认为部分问题可能是Controller类包含Model类。

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 $ b

RootScene.h

#import "cocos2d.h" @class Controller; @interface RootScene : Scene { Controller *controller; } @property (nonatomic, retain) Controller *controller; @end

RootScene.m / p>

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 / p>

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 / p>

ALayer.m

#import "ALayer.h" @implementation ALayer -(void) draw { // draw based on state of the model } @end

推荐答案

您的实施 AScene 不会 #import 控制器的标题。

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

编辑: 。

在AScene.m中添加:

In AScene.m add:

#import "Controller.h"

更多推荐

可能的循环参考问题

本文发布于:2023-11-07 06:36:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!