Iphone 对象引用

Iphone 对象引用,iphone,objective-c,oop,Iphone,Objective C,Oop,我有一个针对iPhone的情景编程。我有一个包含另一个对象(对象B)的对象(对象A)。有没有办法从对象B引用对象a?如果是这样,我该怎么做?否。对象B需要有自己指向对象A的指针。否。对象B需要有自己指向对象A的指针。您可以让类像这样相互引用: ClassA.h: @class ClassB // let the compiler know that there is a class named "ClassB" @interface ClassA : NSObject { ClassB

我有一个针对iPhone的情景编程。我有一个包含另一个对象(对象B)的对象(对象A)。有没有办法从对象B引用对象a?如果是这样,我该怎么做?

否。对象B需要有自己指向对象A的指针。

否。对象B需要有自己指向对象A的指针。

您可以让类像这样相互引用:

ClassA.h:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end
B.h类:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end
ClassA.m:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end
B.m类:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end

您可以让这些类相互引用,如下所示:

ClassA.h:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end
B.h类:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end
ClassA.m:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end
B.m类:

@class ClassB // let the compiler know that there is a class named "ClassB"

@interface ClassA : NSObject {
    ClassB *objectB;
}

@property (nonatomic, retain) ClassB *objectB;

@end
@class ClassA; // let the compiler know that there is a class named "ClassA"

@interface ClassB : NSObject {
    ClassA *objectA;
}

@property (nonatomic, assign) ClassA *objectA; // "child" object should not retain its "parent"

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

@synthesize objectB;

- (id)init {
    if (self = [super init]) {
        objectB = [[ClassB alloc] init];
        objectB.objectA = self;
    }

    return self;
}

@end
#import "ClassA.h"
#import "ClassB.h"

@implementation ClassB;

@synthesize objectA;

- (id)init {
    if (self = [super init]) {
        // no need to set objectA here
    }

    return self;
}

@end

那起作用了——谢谢你。一个问题是@class指令行应该有一个分号,比如
@ClassA+1,除非我将B的属性设置为
assign
而不是
retain
,只是为了避免一个保留周期。将objectA属性(在ClassB中)更改为“assign”。这很有效-谢谢。一个问题是@class指令行应该有一个分号,比如
@ClassA+1,除非我将B的属性设置为
assign
,而不是
retain
,只是为了避免一个保留周期。将objectA属性(在ClassB中)更改为“assign”。这个问题标题的措辞是否更好?这正是我要找的,但标题让我很难找到。我没有足够的代表(我想?)来编辑其他人的问题-或者这是一个mod的力量?这个问题标题可以用更好的措辞吗?这正是我要找的,但标题让我很难找到。我没有足够的代表(我想?)来编辑其他人的问题——或者这是一种mod能力?