Lost Sorcerer
Lost Sorcerer

Reputation: 915

Class type confliction ios

I don't know if it is a flaw in XCode or my lack of experience in Obj-C but I have run into an issue trying to make properties in one class that contains the pointer to the other, while the other class has a property containing a pointer to the first class.

I know the above may not be clear but hopefully this example shows what I mean:

//Class Journal.h

@property (strong) JournalVC *JVC

//Class JournalVC.h
//IVar
Journal *_Journal

In both classes I import the header of the other so XCode knows the class of the other. If I dont import the headers it complains it does not know what type the property/IVar should be and wants to replace it with the class it is(JournalVC as Journal vice versa). This also happens when both classes have the import statement for the header of the other.

This does not happen if only one knows of the other. It works when JournalVC is replaced with UIVIewController(its parent class) and no import statement, while the IVar remains the same and that header imports the other.

I think the problem is that both headers are importing each other, which causes confusion with XCode. Is there any other way to let one of the header files know of the class of the property without this issue?

Upvotes: 2

Views: 207

Answers (1)

Tim
Tim

Reputation: 60130

You can use the @class forward declaration. Don't make a circular header import; instead, just declare the Journal class in the JournalVC header with the line:

@class Journal;

Then, do the traditional header import in JournalVC.m. See this question for more info.

Upvotes: 5

Related Questions