一.在.h頭文件中盡量少引用其他.h頭文件

1.好處:a減少編譯時間;b.避免循環引用(如果用#include的話);c.減少依賴關系容易做SDK,供第三方調用
2.正確的例子(原則就是能不在頭文件引用,就不在頭文件引用)
a..h文件先用@class聲明,.m文件里面在引入

//
//  AppDelegate.h
//
#import <UIKit/UIKit.h>
@classHuViewController;@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) HuViewController  *viewController;
@end

//使用的地方

//  AppDelegate.m
#import "HuViewController.h"
@interfaceAppDelegate ()
@end
@implementation AppDelegate
@end

b.如果需要引入協議,最好將協議單獨放在一個頭文件里(也便于復用)

//  HsTradeGpzyDefine.h
@class  HsGpzySelectContractPage;@protocol HsGpzySelectContractPageDelegate <NSObject>@optional- (void)tradeSelectContractPage:(HsGpzySelectContractPage *)page submitData:(id)data;
@end

//使用的地方

//  HsGpzySelectContractPage.m
#import "HsTradeGpzyDefine.h"
@interfaceHsGpzySelectContractPage ()
@property(nonatomic, assign) id<HsGpzySelectContractPageDelegate> delegate;
@end

c.如果委托類型協議不能單獨放在一個文件里(因為要和委托類放一起)

/// @文件名稱  HsPickerView.h
@protocol HsPickerViewDelegate <NSObject>@optional-(void)onSelectResponse:(NSDictionary*)params;
@end

@interface HsPickerView : UIPickerView<UIPickerViewDelegate,UIPickerViewDataSource> 
{}
@property(nonatomic, assign) id<HsPickerViewDelegate> backDelegate;
@end

//使用的地方

//  HsTradeGpzyCoverDetailPage.m
@interfaceHsTradeGpzyCoverDetailPage ()<HsPickerViewDelegate>
@property(nonatomic, retain) HsPickerView *supplyStylePickerView;
@end
@implementation HsTradeGpzyCoverDetailPage
- (void) onSelectResponse:(NSDictionary*)params{   
}
@end

如果你發現本文對你有所幫助,如果你認為其他人也可能受益,請把它分享出去

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容