obj-cで、次のようにプロパティから親インスタンスにアクセスできるかどうか疑問に思います。
@interface AObject : NSObject {
}
-(void) sample{
NSLog("%@", [[self parentInstance] Id]);
}
@interface DbObject : NSObject {
NSInteger Id;
AObject* ob;
}
obj-cで、次のようにプロパティから親インスタンスにアクセスできるかどうか疑問に思います。
@interface AObject : NSObject {
}
-(void) sample{
NSLog("%@", [[self parentInstance] Id]);
}
@interface DbObject : NSObject {
NSInteger Id;
AObject* ob;
}
私が知っているわけではありません。AObject にその親を認識させる必要があります。ランタイムをいじれば他の方法もあるかもしれませんが、AObject に親を要求させる方が簡単です。
あなたが何を求めているのか完全にはわかりません。superキーワードをお探しですか?
本当にこれを行いたい場合は、AObject の「retain」メソッドをオーバーライドして、現在のスタックを調べ、呼び出し元のクラスの ID を探すことができます。申し訳ありませんが、それを行う方法を調査する必要があります。他の調査からできることを知っています.
あなたが実際にやろうとしているのが、特定のオブジェクトを誰が所有しているかを知らせるある種のデバッグ ステートメントを持つことである場合は、retain をオーバーライドしてブレークポイントを設定するか、そこに役立つデータ ダンプを配置することができます。
いいえ、スーパーはクラスを取得するためのものです。
私が求めているのは、親オブジェクトを取得する方法です(ツリーのように)。
しかし、それは不可能だと思います...そして、子供を親と関連付ける必要があります.
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;
}