0

The following is my AppDelegate.h file:

#import <UIKit/UIKit.h>    

@class ViewController;  

@interface AppDelegate : UIResponder <UIApplicationDelegate>    
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@end

The following is a file called ViewControllerTEST.h containing the declaration of the ViewController interface used by AppDelegate.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

My question:

How does the compiler know where to find the declaration for the ViewController interface when "@class ViewController" line in AppDelegate.h is ran? In other words, how did the compiler know that the interface for the ViewController class is defined in ViewControllerTEST.h when it's executing the line "@class ViewController" in AppDelegate.h?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
BlueBucket7
  • 185
  • 3
  • 13

1 Answers1

0

It looks like someone renamed the file that is now called ViewControllerTEST.h file. It was most probably originally called ViewController.h.

By default, if your header or implementation file has a line like the following: "@interface ViewController : ..." then the actual .h or .m file will be named ViewController.h or ViewController.m. Such is the case when you create a new class (File -> New -> File -> Objective C Class).

Anyway, you can actually change the filenames of these .h and .m files after creating them and still maintain the connections (don't forget to change the corresponding "#import ..." portion in the .m file though). What's important is that you do not change the controller name in the "@interface ..." and "@implementation ..." portions so that they are two differently named view controllers. This will yield errors in Xcode.

Having said all of this, Xcode keeps track of the actual class name ("ViewController") when you first created the class. Unless you perform Refactor in Xcode, which is not an important topic at this point in your learning.

Hope this helps...

Koh
  • 1,570
  • 1
  • 8
  • 6
  • So does this mean internally Xcode matches the class name from @class ViewController to a file (that was created originally) named ViewController.h? Even if ViewController.h changed into another name? – BlueBucket7 Sep 10 '14 at 17:25
  • If so, how is it done internally? This seems dangerous...Thanks for the help! – BlueBucket7 Sep 10 '14 at 17:31