Objective C 中的循环依赖解决方法
我有一个定义如下的协议:
@protocol Container
- (BOOL)putStuff: (Stuff *) theStuff;
@end
和带有以下声明的类:
@interface Stuff : NSObject {
}
@property(readwrite,nonatomic,retain) NSObject <Container> * containerHoldingMe;
@end
我在 putStuff 声明上收到错误 - “预期类型”。然而,如果我将该方法的参数更改为 NSObject,它就会编译正常 — 所以,我认为 Objective C 不允许循环依赖。
在我正在工作的实际课程中,我想实现两个 - 但是,如果没有它,我如何实现这样的逻辑?
I have a protocol that is defined like this:
@protocol Container
- (BOOL)putStuff: (Stuff *) theStuff;
@end
and class with following declaration:
@interface Stuff : NSObject {
}
@property(readwrite,nonatomic,retain) NSObject <Container> * containerHoldingMe;
@end
and I get an error on putStuff declaration — "Expected a type". If I change the argument of the method to NSObject, however, it compiles OK — so, I think that Objective C just doesn't allow loop dependence.
In real classes that I'm working at I want to implement two- However, how do I implement such logic without it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当编译器评估您的
Container
协议时,它并不知道您的Stuff
类。您可以使用前向声明让编译器了解您的Stuff
类:By the time the compiler evaluates your
Container
protocol, it does not know about yourStuff
class. You can let the compiler know about yourStuff
class with a forward declaration:您应该在此处使用
id
。You should use
id
here.