以下是我的AppDelegate.h文件:
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
以下是一个名为ViewControllerTEST.h的文件,其中包含AppDelegate.h使用的ViewController接口的声明:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
我的问题:
当运行AppDelegate.h中的“@class ViewController”行时,编译器如何知道在哪里可以找到ViewController接口的声明?换句话说,编译器在AppDelegate.h中执行“@class ViewController”行时,是如何知道ViewController类的接口是在ViewControllerTEST.h中定义的?
看起来有人重命名了现在称为ViewControllerTEST.h文件的文件。它最初可能最初称为ViewController.h。
默认情况下,如果您的标头或实现文件有如下所示的行:“@ interface ViewController:...”那么实际的.h或.m文件将命名为ViewController.h或ViewController.m。创建新类时就是这种情况(文件 - >新建 - >文件 - >目标C类)。
无论如何,你可以在创建它们之后实际更改这些.h和.m文件的文件名,并保持连接(不要忘记更改.m文件中相应的“#import ...”部分)。重要的是你不要在“@interface ...”和“@implementation ...”部分更改控制器名称,这样它们就是两个不同命名的视图控制器。这将在Xcode中产生错误。
说完所有这些之后,Xcode会在您第一次创建类时跟踪实际的类名(“ViewController”)。除非你在Xcode中执行Refactor,这在你学习的这一点上不是一个重要的主题。
希望这可以帮助...