在 obj-c 中是否可以获取属性的父实例?

发布于 2024-07-12 08:12:21 字数 226 浏览 5 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娜些时光,永不杰束 2024-07-19 08:12:22

据我所知。 您必须让 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.

追星践月 2024-07-19 08:12:22

您必须使 AObject 知道 DbObject,而且还应该使用选择器访问 DbObject 的属性。

@interface AObject : NSObject
{
    id father;
}
- (id) initWithFather:(id) theFather;
- (void) sample;
@end


@implementation AObject
- (id) initWithFather:(id) theFather
{
    father = theFather;
    return self;
}    

- (void) sample
{
    NSLog (@"%d", [father Id]);
}
@end


@interface DbObject : NSObject
{
    NSInteger Id;
}
- (id) initWithId:(NSInteger) anId;
- (NSInteger) Id;
@end


@implementation DbObject
- (id) initWithId:(NSInteger) anId;
{
    Id = anId;
    return self;
}

- (NSInteger) Id
{
    return Id;
}
@end


int main (int argc, char *argv[])
{
    DbObject *myDbObject = [[DbObject alloc] initWithId:5];
    AObject *myAObject = [[AObject alloc] initWithFather:myDbObject];

    [AObject sample];

    return 0;
}

You would have to make AObject aware of DbObject, but also you should access the properties of DbObject using selectors.

@interface AObject : NSObject
{
    id father;
}
- (id) initWithFather:(id) theFather;
- (void) sample;
@end


@implementation AObject
- (id) initWithFather:(id) theFather
{
    father = theFather;
    return self;
}    

- (void) sample
{
    NSLog (@"%d", [father Id]);
}
@end


@interface DbObject : NSObject
{
    NSInteger Id;
}
- (id) initWithId:(NSInteger) anId;
- (NSInteger) Id;
@end


@implementation DbObject
- (id) initWithId:(NSInteger) anId;
{
    Id = anId;
    return self;
}

- (NSInteger) Id
{
    return Id;
}
@end


int main (int argc, char *argv[])
{
    DbObject *myDbObject = [[DbObject alloc] initWithId:5];
    AObject *myAObject = [[AObject alloc] initWithFather:myDbObject];

    [AObject sample];

    return 0;
}
芯好空 2024-07-19 08:12:22

我不完全确定你在问什么。 您是否在寻找 super 关键字?

I'm not entirely sure what you're asking. Are you looking for the super keyword?

π浅易 2024-07-19 08:12:22

不,超级是为了上课。

我问的是如何获取父对象(就像在树中一样)。

但我认为这是不可能的......并且有必要将孩子与父母联系起来。

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.

作死小能手 2024-07-19 08:12:22

如果你确实想这样做,你可以重写 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文