在 obj-c 中是否可以获取属性的父实例?
我想知道 obj-c 是否可以从属性访问父实例,例如:
@interface AObject : NSObject {
}
-(void) sample{
NSLog("%@", [[self parentInstance] Id]);
}
@interface DbObject : NSObject {
NSInteger Id;
AObject* ob;
}
I wonder if is possible in obj-c to acces the parent instance from a property, like:
@interface AObject : NSObject {
}
-(void) sample{
NSLog("%@", [[self parentInstance] Id]);
}
@interface DbObject : NSObject {
NSInteger Id;
AObject* ob;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知。 您必须让 AObject 知道它的父级。 如果您摆弄运行时,可能还有其他方法可以做到这一点,但让 AObject 需要父级会更容易。
Not that I'm aware of. You would have to make AObject aware of it's parent. There may be other ways to do it if you fiddle with the runtime, but it's just easier to make AObject require a parent.
您必须使
AObject
知道DbObject
,而且还应该使用选择器访问DbObject
的属性。You would have to make
AObject
aware ofDbObject
, but also you should access the properties ofDbObject
using selectors.我不完全确定你在问什么。 您是否在寻找
super
关键字?I'm not entirely sure what you're asking. Are you looking for the
super
keyword?不,超级是为了上课。
我问的是如何获取父对象(就像在树中一样)。
但我认为这是不可能的......并且有必要将孩子与父母联系起来。
No, the super is for get the class.
I'm asking is in how get the parent object (like in a tree).
But I think that is not possible... and is necesary associate the child with the parent.
如果你确实想这样做,你可以重写 AObject 中的“retain”方法来查看当前堆栈并查找调用类的 ID。 抱歉,您必须研究如何做到这一点,我只知道您可以从其他研究中获得。
如果您真正想做的是使用某种调试语句来告诉您谁拥有特定对象 - 您可以覆盖保留并设置断点或将有用的数据转储放在那里。
If you really want to do this, you could override the "retain" method in AObject to look at the current stack and look for the ID of the calling class. Sorry, you'll have to investigate how to do that, I just know you can from other research.
If what you are really trying to do is have some kind of debug statement that tells you who owns a particular object - you can just override retain and set breakpoints or put helpful data dumps in there.