iOS - AppDelegate 中的头文件太多?

标签 ios delegates header

我只是想知道将大量头文件导入 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/

相关文章:

iphone - 如何在 ios 中加载 wfm(线框)

ios - iOS App 的安全设计

ios - 使用 dismissViewControllerAnimated 传回数据

python - PySide 中的自定义委托(delegate)

javascript - 未处理的 promise 拒绝: Can't set headers after they are sent

ios - 向 UITableViewCell 添加边距

ios - 在 iPhone7.1.1 上测试时没有这样的模块 AVFoundation

design-patterns - 您是否在具有闭包/委托(delegate)/函数指针的编程语言中使用模板方法模式?

c++ - C++1y 模式下的 Clang >= 3.3 无法解析 <cstdio> header

perl - 在 perl 中设置 http header 以强制下载内容