1.好處:a減少編譯時間技即;b.避免循環(huán)引用(如果用#include的話)泵肄;c.減少依賴關(guān)系容易做SDK光督,供第三方調(diào)用
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.如果需要引入?yún)f(xié)議,最好將協(xié)議單獨放在一個頭文件里(也便于復(fù)用)
// 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.如果委托類型協(xié)議不能單獨放在一個文件里(因為要和委托類放一起)
/// @文件名稱 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
如果你發(fā)現(xiàn)本文對你有所幫助,如果你認(rèn)為其他人也可能受益钾军,請把它分享出去