views:

136

answers:

2

Hey, My head is about to explode with this logic, can anyone help?

Class A #imports Class B. Class A calls Method A in Class B.

This works great

Class B wants to send a response back to Class A from another method that is called from Method A. If you #import Class A from Class B, it is in effect an infinite loop and the whole thing crashes.

Is there a way to do this properly, like a parent type thing?

BTW, I'm developing for iPhone.

EDIT When i #import Class A from Class B i get this compiler error...

error: expected specifier-qualifier-list before 'StoreHoursCore'

Any ideas?

+7  A: 

I believe that using the @class directive should solve your issue.

Using @class forward declares your class type in order that the compiler doesn't cough.

Take a look at this question on SO.

Jacob Relkin
I would add that this is called "forward declararion"
Vladimir
you keep typing the same thing seconds before I do :/
Brock Woolf
@Vladimir, edited it, thanks!
Jacob Relkin
@Brock, who? Me, or @Vladimir?
Jacob Relkin
You Jacob. but its all good :)
Brock Woolf
@Brock, I'm creeping up on you in rep points too lol!
Jacob Relkin
@Jacob Lol. Yeah what a coincidence!
Brock Woolf
I would add that it's good practice to use #import only in the implementation file and to use the @class otherwise. The exception to this is when you have to import the header for a super class. It's not a hard and fast rule but it is the habit you should get into.
TechZen
Added the error...
tarnfeld
@tarnfeld, can you post some more code?
Jacob Relkin
+3  A: 

Use @classto forward declare it as a class. This hints the compiler to know that it is a class, without having to cross include and compile the class multiple times (the reason you are getting your error). ie:

@class myClass;

The #importpreprocessor directive has protection against multiple includes, but the #include directive does not.

Brock Woolf