How can I have references between two classes in Objective-C?

I am developing an application for the iPhone, and I am new to Objective-C, as well as to class.h and class.m.

Now I have two classes that must have a variable of a different type. But it just seems impossible.

If in class1.m (or class2.m) I include class1.h and then class2.h, I cannot declare class2 variables in class1.h, if I include class2.h and then class1.h, I cannot declare class1 variables in class2.h.

I hope you have an idea because it drives me crazy. Is it really impossible to do?

Thanks.

+5
source share
2 answers

@class - . #import .

Class1.h

@class Class2;

@interface Class1
{
    Class2 * class2_instance;
}
...
@end

Class2.h

@class Class1;

@interface Class2
{
    Class1 * class1_instance;
}
...
@end

, #import .m

+20

. , . , , , .

, ?

+3

All Articles