0

I get the expected specifier-qualifier-list error and upon searching, some people describe cyclic dependency. I'm not sure what that means and could use an explanation if possible. Basically I have a class Foo that uses a Bar type in the .m file. I don't need an ivar of Bar. I thought that I could #import "Bar.h" in the Foo.h file since Foo.m imports the Foo.h file, but that's where I get the error. Why do you put it in the .m file in this case, and not just include everything in the .h file to make things more clean at the top of the file? Sorry if this is a dumb question. Thanks.

Crystal
  • 28,460
  • 62
  • 219
  • 393
  • Go through following links http://stackoverflow.com/questions/3485314/how-do-i-resolve-this-circular-dependency and http://stackoverflow.com/questions/5415143/cyclic-dependencies – mrugen munshi Jul 22 '11 at 18:24

1 Answers1

3

A Cyclic Dependency is exactly what it sounds like. You definitely have one.

Look at it like this:

Foo needs Bar, but Bar needs Foo.

So, you instantiate Foo, it gets a reference to Bar. Well this instantiates Bar, so this gets a reference to Foo. This instantiates Foo again, so it gets a reference to Bar. Welp, this Bar needs a Foo, so it goes and gets a reference to Foo.

This keeps happening over and over and over, essentially a infinite loop. To get past a cyclic dependency, you tell Foo about Bar

//Foo.h
@class Bar

@interface Foo: NSObject {

}

@end

Then, import the header of Bar in Foo.m

//Foo.m
#import "Foo.h"
#import "Bar.h"

@implementation Foo
-(void)barTime {
     Bar bar = [[[Bar alloc] init] autorelease];
     [bar getDrunk];
}

@end

This breaks the chain. Foo just knows about Bar, so only Bar gets Foo.

ColdLogic
  • 7,206
  • 1
  • 28
  • 46