Cyclic dependency? "expected specifier-qualifier-list"
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 possibl开发者_JS百科e. 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.
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.
精彩评论