iOS - AppDelegate 中的头文件太多?

发布于 2024-12-29 20:09:53 字数 192 浏览 0 评论 0原文

我只是想知道将大量头文件导入 AppDelegate 是否被认为是不好的做法?

我的游戏有很多视图(在单独的 .xib 文件中),AppDelegate 在这些视图之间切换。目前,我正在 AppDelegate.h 文件中导入 16 个头文件,但有没有更好的方法来管理所有这些?我见过的大多数示例代码最多有 4 或 5 个头文件。

谢谢!

I was just wondering if it's considered bad practice to have a large number of header files imported into an AppDelegate?

My game has a lot of views (in separate .xib files) that the AppDelegate switches between. At the moment, I am importing 16 header files in my AppDelegate.h file, but is there a better way to manage all of this? Most sample code I've seen has a maximum of around 4 or 5 header files.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2025-01-05 20:09:53

通常更好的做法是在头文件中转发声明您的类,然后在实现文件中导入它们的头,例如:

// .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"

It's usually better practice to forward declare your classes in the header file, and then import their headers in your implementation file, for example:

// .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

Doing this will help to avoid situations where you will end up with circular #import references.

I will also often create a header file simply for importing other header files for clarity, e.g. #import "MyViews.h"

毁我热情 2025-01-05 20:09:53

示例代码就是这样的——示例代码,通常旨在清楚地演示一个概念。如果您的应用程序执行不止一件相对简单的事情,则不要期望它看起来像示例代码。期望它看起来更像是几十个示例项目放在一起(尽管我不建议完全这样做!)。

Sample code is just that -- sample code, usually meant to clearly demonstrate a single concept. Don't expect your application to look like sample code if it does more than one single relatively simple thing. Expect it to look more like several dozen sample projects all put together (although I wouldn't advise doing exactly that!).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文