0

my xcode 4 just crashed and I get build errors:

Unknown type name 'SecondViewController'

in the @interface code block:

SecondViewController *sms; 

and

Property with 'retain' attribute must be object type

in

@property(nonatomic,retain) SecondViewController *sms; 

However I import SecondViewController.h. The same code worked before the crash.

FirstViewController.h: http://pastebin.com/jnPKBny7

SecondViewController.h: http://pastebin.com/2D058ZAK

Edit: I realized that this error occures because the classes import each other. Can anyone describe why this is wrong?

Any ideas?

2
  • Can you post the SecondViewController header file and the file where you are getting these errors? Commented Apr 30, 2011 at 16:10
  • 1
    Does the SecondViewController.m show up in Project navigator -> first entry/your project -> right window Targets -> Build Phases -> Compile Sources? Commented Apr 30, 2011 at 17:34

1 Answer 1

1

You can't have circular imports. I think it's good practice to use forward class declarations with the @class directive whenever possible. For your case:

FirstViewController.h:

@class SecondViewController ; @interface FirstViewController { SecondViewController * _secondViewController ; } @property ( nonatomic, retain ) secondViewControlller ; @end 

SecondViewController.h:

@class FirstViewController ; @interface SecondViewController { FirstViewController * _firstViewController ; } @property ( nonatomic, retain ) firstViewControlller ; @end 

Then in your .m files, import the .h files for the classes you are using. The only reasons to import .h files into other .h files are:

  • framework imports (i.e. )
  • you need to use a protocol
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.