我只是想知道将大量头文件导入 AppDelegate 是否被认为是不好的做法?
我的游戏有很多 View (在单独的 .xib 文件中),AppDelegate 在这些 View 之间切换。目前,我正在 AppDelegate.h 文件中导入 16 个头文件,但有没有更好的方法来管理所有这些?我见过的大多数示例代码最多有 4 或 5 个头文件。
谢谢!
最佳答案
通常更好的做法是在头文件中转发声明类,然后在实现文件中导入它们的头,例如:
// .h
#import <UIKit/UIKit.h>
@class MyView;
@class MyOtherView;
@class MyOtherOtherView;
@interface MyAppDelegate : NSObject <UIApplicationDelegate>
@property (strong, nonatomic) MyView *myView;
@property (strong, nonatomic) MyOtherView *myOtherView;
@property (strong, nonatomic) MyOtherOtherView *myOtherOtherView;
@end
// .m
#import "MyAppDelegate.h"
#import "MyView.h"
#import "MyOtherView.h"
#import "MyOtherOtherView.h"
@implementation MyAppDelegate
@synthesize myView;
@synthesize myOtherView;
@synthesize myOtherOtherView;
// methods
@end
这样做将有助于避免出现循环 #import
引用的情况。
为了清楚起见,我还经常创建一个头文件,只是为了导入其他头文件,例如#import“MyViews.h”
关于iOS - AppDelegate 中的头文件太多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9058303/