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
精彩评论