我只是想知道如果大量的頭文件導入AppDelegate被認爲是不好的做法嗎?iOS - AppDelegate中頭文件過多?
我的遊戲有許多AppDelegate之間切換的視圖(在單獨的.xib文件中)。目前,我在AppDelegate.h文件中導入了16個頭文件,但有沒有更好的方法來管理所有這些?我見過的大多數示例代碼最多有大約4或5個頭文件。
謝謝!
我只是想知道如果大量的頭文件導入AppDelegate被認爲是不好的做法嗎?iOS - AppDelegate中頭文件過多?
我的遊戲有許多AppDelegate之間切換的視圖(在單獨的.xib文件中)。目前,我在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"
「將大量的頭文件導入到AppDelegate中被認爲是不好的做法?」 - 不,它的做法不錯;導入你需要的東西,除此之外別無它物。 – chown 2012-01-30 01:06:46