質問

私はバカではありませんが、ヘッダーファイルを使用すると、ときどき1つのように感じます。おそらく、私が解決できないエラーを含む、非常に複雑なセットアップがあります。ここでは、私ができる限りの詳細と同じくらい簡単です。...

  • Modelクラスを含むControllerクラスがあります。
  • アクションをキャプチャしてControllerと通信するSceneクラスがあります。
  • Modelクラスと通信してModelの状態を出力するLayerクラスがあります。
  • Sceneクラスには、出力専用のLayerクラスが含まれています。
  • シーンは、Cocos2Dフレームワークによって決定されたレイヤーを保持する必要があります。
  • 特定のSceneクラスは、Controllerクラスへの参照を保持するRootSceneクラスから派生しています。
  • 特定のLayerクラスは、Modelクラスへの参照を保持するRootLayerクラスから派生します。
  • コントローラーはシーンの作成を担当し、シーンはレイヤーの作成を担当します。

問題は、レイヤーを作成し、コントローラーのモデルをレイヤーのモデルに渡すときに発生します( AScene.m 内)。 「構造またはユニオンではない何かのメンバー「モデル」のリクエスト」を受け取ります。キャストが機能せず、これらのクラスが相互に対話できるようにする方法に途方に暮れています。問題の一部は、ControllerクラスにModelクラスが含まれている可能性があると思います。

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
役に立ちましたか?

解決

AScene の実装は、 Controller のヘッダーを #import しません。

編集:明示的な解決策。

AScene.mに追加:

#import "Controller.h"

他のヒント

ニコライは正しいようです。それを超えて、ベストプラクティスは、プロパティ(ivarsなど)を大文字で開始しないように指示します。あなたはただ傷を求めているだけです。代わりにこれらを使用してください:

Controller.h

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

Controller.m

@synthesize model;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top