Category,protocol,Block总结对比
来源:互联网 发布:highcharts 更新数据 编辑:程序博客网 时间:2024/06/05 02:46
Category、protocol、Block对比:
3者都是想要实现某个方法,但是它们实现方法的途径是不同的;
Category是在类中添加方法,类扩展方法,实现方法;
protocol是通过代理来实现其他协议里的方法;
Block是在方法原来的基础上,增加代码块,方法扩展方法,实现方法;
还有继承,继承是通过子类继承父类,子类扩展方法,实现方法。
Category和继承对比:
继承比Category繁琐麻烦,但Category不能为被扩展的类添加实例变量,
如果需要添加一个新的变量,则需用继承;如果只是添加一个新的方法,用Category比较好。
一、Category
Objective-C提供了一个非常灵活的类(Class)扩展机制——类别(Category)。
Category用于对一个已经存在的类添加方法,实现想要的功能,你只需要知道这个类的公开接口,不需要知道类的源代码。
使用Category:
1、创建一个iOS Single View Application名为Demo。然后为创建一个NSString类的Category扩展。File > New File > Objective-C File > File命名为Dm,File Type选Category,Class选NSString ,然后系统会自动生成一个固定格式ClassName+CategoryName的.h和.m文件,即NSString+Dm.h和NSString+Dm.m
2、在NSString+Dm.h中声明Category
#import <Foundation/Foundation.h>@interface NSString (Dm)+ (NSString*)Dm:(NSString*)strDm;@end
#import"NSString+Dm.h"@implementationNSString (Dm)+ (NSString*)Dm:(NSString*)strDm{ //编写代码,实现方法}@end
二、protocol
protocol(协议)和delegate(委托)是联系在一起的,关于它们的关系,有个形象的例子:比如我上班的工作主要内容包括(1)写代码(2)接电话,(1)我自己全权负责了,(2)我不想或者不方便自己做,所以我想找个代理(delegate) 帮我做这些事,于是我定了一个招聘要求(Protocol),里面写明我的代理需要会做(2)这件事,很快,我找到了一个代理。于是以后遇到打电话的活,我就把它转交给代理(delegate)去处理。
代码实例:
1、定义。在.m中定义一个Textfield名称为userName
@interface ViewController ()@property (weak, nonatomic) IBOutlet UITextField *userName;@end
2、找方法。在UITextField中找方法,看到textFieldDidBeginEditing方法是userName想要的,可以帮助userName实现某些功能,这个方法在UITextFieldDelegate协议中
@protocol UITextFieldDelegate <NSObject>//protocol里面有许多方法,@required是必须实现的,@optional是实现不实现都可以的。@optional- (void)textFieldDidBeginEditing:(UITextField *)textField;@end
3、声明。在.h中添上<UITextFieldDelegate>,要使用textFieldDidBeginEditing方法,首先要遵守UITextFieldDelegate协议
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>@end
4、代理。在.m中创建代理,然后通过代理来调用方法
@implementation ViewController- (void)viewDidLoad { [super viewDidLoad];//创建代理的格式:XXX.delegate = 代理; _userName.delegate = self;//通过代理来调用方法,传入参数。 [self textFieldDidBeginEditing:_userName]; }
5、实现。在方法中编写代码,帮助userName实现想要的功能
-(void)textFieldDidBeginEditing:(UITextField *)textField{CGRect frame = _userName.frame;int offset = frame.origin.y + frame.size.height - (self.view.frame.size.height - 216.0 - 38.0);if (offset > 0) {self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);[UIView commitAnimations];}}
三、Block
Block(代码块)就是一个函数体,在方法原来的基础上,增加代码块,就可以像调用其他标准函数一样,传入参数,得到返回值,扩展方法。
代码实例:
//1.写一个方法Calculate,传入2个参数,目的是:得到2个参数的和。- (NSInteger) Calculate: (int)first secondNum:(int)second{ return first + second;} //2.这时还是想利用Calculate方法,传入2个参数,目的是:得到2个参数的差。这时就可以在原来的基础上,增加代码块block,回调需要的方法。- (NSInteger) Calculate: (int)first secondNum:(int)second testBlock:(void (^)(int result)) callback{ //—————————————————— 在原来的基础上 ————————————————————— 代码块名称:(返回值(^代码块标识符)(参数类型 参数))回调的方法名 int result1 = first - second; //3.回调想要的方法,从而result1赋值给result callback(result1); int result2 = first + second; //4.返回值 return result2;}- (void)test { //5.调用的是第一个方法,所以结果为5 NSLog(@"%d",[self Calculate:2 secondNum:3]); NSInteger a1=[self Calculate:6 secondNum:2 testBlock:^(int result) { //6.有代码块testBlock,调用的是第二个方法,所以result结果为4 NSLog(@"%d", result); }]; //7.第二个方法的返回值是result2,result2赋值给a1,所以a1=8 NSLog(@"a1=%d",a1);}
四、总结
(1)Category、protocol、Block对比:
3者都是想要实现某个方法,但是它们实现方法的途径是不同的;
Category是在类中添加方法,类扩展方法,实现方法;
protocol是通过代理来实现其他协议里的方法;
Block是在方法原来的基础上,增加代码块,方法扩展方法,实现方法;
还有继承,继承是通过子类继承父类,子类扩展方法,实现方法。
(2)Category和继承对比:
继承比Category繁琐麻烦,但Category不能为被扩展的类添加实例变量,
如果需要添加一个新的变量,则需用继承;如果只是添加一个新的方法,用Category比较好。
- Category,protocol,Block总结对比
- category、block、protocol、SEL
- Category Extension protocol 总结
- ios笔记--class关键字,Category,protocol和block笔记
- 黑马程序员-----Category,protocol和block(一)
- 黑马程序员-----Category,protocol和block(二)
- 黑马程序员-----Category,protocol和block(三)
- 黑马程序员-----Category,protocol和block(四)
- 黑马程序员---OC基础---Block、分类Category、协议protocol
- Objective-C-----协议protocol,代码块block,分类category
- OC学习总结之block、category
- OC语言BLOCK和Protocol总结
- block protocol
- Category / Extensions / Protocol /Informal Protocol
- 黑马程序员——OC热身——Category、Protocol、Block、常用结构体
- 黑马程序员——OC基础:block和Protocol及Category
- 黑马程序员——ios知识整理——category、block、protocol
- Category vs Protocol
- 取得颜色
- CodeForces 295A Greg and Array(线段树)
- HDU1570水题
- Unreal4 C++使用注意事项
- 【Python】Python处理Excel
- Category,protocol,Block总结对比
- Java集合 -Collection
- IOS NavigationBar 背景色
- FILE总结
- Dos命令查看端口占用及关闭进程
- 写在开始
- iOS咱们一起学吧
- Esxi虚拟机克隆的问题
- python机器学习库scikit-learn