开发者

Cocoa Controller imports Model, Model imports Controller --> Exception

why isn't it possib开发者_运维百科le in Cocoa that two Classes both import each other? I tried the following code:

Controller.h:

#import <Cocoa/Cocoa.h>
#import "Model.h"

@interface Controller : NSObject {
 Model *model;
}

@end

Model.h:

#import <Cocoa/Cocoa.h>
#import "Controller.h"

@interface Model : NSObject {
 Controller *controller;
}

@end

which raises the following exceptions:

error: expected specifier-qualifier-list before 'Controller'
error: expected specifier-qualifier-list before 'Model'

Can someone please explain why this is?

Thanks! xonic


Explain why? No.

But the solution is to use the @class declaration like so:

@class Model;
@interface Controller : NSObject {
 Model *model;
}
@end


A solution for this is that: Model.h:

#import <Cocoa/Cocoa.h>
#import "Controller.h"

@class Controller;
@interface Model : NSObject {
 Controller *controller;
}

@end

And you done with that

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜