如何让协议的函数非公开
如果我想让遵守协议的Object,其协议方法为『非公开实现』,只需要在「.m」文件里的interface后声明遵守协议。
//FatherObject.h
#import <Foundation/Foundation.h>
//协议
@protocol FatherDelegate <NSObject>
@required
- (void)doDelegateMthod;
@end
//父类
@interface FatherObject : NSObject
@property (nonatomic, weak) id<FatherDelegate> delegate;
@end
//FatherObject.m
#import "FatherObject.h"
//在此处的Extends后声明遵守协议
@interface FatherObject () <FatherDelegate>
@end
@implementation FatherObject
- (void)doDelegateMthod {
NSLog(@"Do Delegate Method In Father");
}
@end
如何让协议的函数公开
如果我想让遵守协议的Object,其协议方法为『公开实现』,只需要在「.h」文件里的interface后声明遵守协议。
//FatherObject.h
#import <Foundation/Foundation.h>
//协议
@protocol FatherDelegate <NSObject>
@required
- (void)doDelegateMthod;
@end
//父类
//在此处的Interface后声明遵守协议
@interface FatherObject : NSObject <FatherDelegate>
@property (nonatomic, weak) id<FatherDelegate> delegate;
@end
//FatherObject.m
#import "FatherObject.h"
@implementation FatherObject
- (void)doDelegateMthod {
NSLog(@"Do Delegate Method In Father");
}
@end