ios 中Category类别(扩展类)小结
类别
类别是一种为现有的类添加新方法的方式。
利用Objective-C的动态运行时(runtime)分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为任何类添加新的方法,包括那些没有源代码的类。
类别使得无需创建对象类的子类就能完成同样的工作。
一、创建类别
1、声明类别
声明类别与声明类的形式很相似:
@interface NSString(NumberConvenience)
-(NSNumber *)lengthAsNumber;
@end//NumberConvenience
这个声明有两个特点:
(1)现有的类位于@interface关键字之后,其后是位于圆括号中的类别名称。类别名称是NumberConvenience,而且该类别将向NSString类中添加方法。换句话说:“我们向NSString类中添加一个名称为NumberConvenience的类别。”
同名类别有唯一性,但是可以添加任意多的不同名类别。
(2)可以执行希望向其添加类别的类以及类别的名称,还可以列出添加的方法
不可以添加新的实例变量,类别生命中没有实例变量部分。
2、实现类别
@implementation NSString(NumberConvenience)
-(NSNumber *)lengthAsNumber
{
unsigned int length = [self length];
return ([NSNumber numberWithUnsignedInt : length]);
} //lengthAsNumber
@end //NumberConvenience
在实现部分也包括类名、类别名和新方法的实现代码。
3、类别的局限性
有两方面局限性:
(1)无法向类中添加新的实例变量,类别没有位置容纳实例变量。
(2)名称冲突,即当类别中的方法与原始类方法名称冲突时,类别具有更高的优先级。类别方法将完全取代初始方法从而无法再使用初始方法。
注意:无法添加实例变量的局限可以使用字典对象解决。
4、类别的作用
类别主要有3个作用:
(1)将类的实现分散到多个不同文件或多个不同框架中。
(2)创建对私有方法的前向引用。
(3)向对象添加非正式协议。
二、利用类别分散实现
我们可以将类的接口放入头文件中,从而将类的实现放入.m文件中
但不可以将@implementation分散到多个不同的.m文件中,使用类别可以完成这一工作,
利用类别,可以将一个类的方法组织到不同的逻辑分组中,使编程人员更加容易的阅读头文件
举例代码:
头文件CatagoryThing.h包含类的声明和一些类别,导入Foundation框架,然后带有3个整型变量的声明:
#import<Foundation/Foundation.h>
@interface CategoryThing : NSObject {
int thing1;
int thing2;
int thing3;
}
@end // CategoryThing
类声明之后是3个类别,每个类别具有一个实例变量的访问器,将这些实现分散到不同的文件中:
@interface CategoryThing(Thing1)
- (void) setThing1: (int) thing1;
- (int) thing1;
@end // CategoryThing (Thing1)@interface CategoryThing (Thing2)
- (void) setThing2: (int) thing2;
- (int) thing2;
@end // CategoryThing (Thing2)@interface CategoryThing (Thing3)
- (void) setThing3: (int) thing3;
- (int) thing3;
@end // CategoryThing (Thing3)
注意:1.类别可以访问其继承的类的实例变量;
2.类别的方法具有最高的优先级;
3.类别可以分散到不同文件中,甚至不同框架中。
三、使用类别创建前向引用
如果其他类中的方法未实现,在你访问其他类的私有方法时编译器报错,
这时使用类别,在类别中声明这些方法(不必提供方法实现),编译器就不会再产生警告。
四、非正式协议和委托类别
Cocoa中的类经常使用一种名为委托(delegate)的技术
委托是一种对象,另一个类的对象会要求委托对象执行他的某些操作:
} // main
创建一个NSObject的类别称为“创建一个非正式协议”,因为可以作为任何类的委托对象使用
响应选择器
选择器只是一个方法名称,但它以Objective-C运行时使用特殊方式编码,以快速执行查询
可以使用@selector()预编译指定选择器,其中方法名位于圆括号中。
例如之前的Car类的setEngine:方法的选择器是:@selector(setEngine:)
而Car类的setTire:atIndex;方法的选择器如下所示:@selector(setTire:atIndex;)
NSObject提供了一个名为respondsToSelector方法,该方法询问对象以确定其是否能够响应某个特定的消息
举例代码:
Car *car = [[Car alloc] init];
if([carrespondsToSelector:@selector(setEngine:)]){
NSLog(@"hihi");
}
选择器的其他应用
选择器可以被传递,可以作为方法的参数使用,甚至可以作为实例变量存储
小结
类别提供了向现有类添加新方法的手段,即使没有这些类的源代码
类别可以将对象的实现分散到多个不同的源文件、甚至多个不同的框架中
使用类别可以声明非正式协议,非正式协议是NSObject的一个类别,他可以列出对象能够响应的方法
非正式协议用于委托,委托是一种允许轻松定制对象行为的技术
类别是一种为现有的类添加新方法的方式。
@interface NSString (NumberConvenience)
-(NSNumber *)lengthAsNumber;
@end
(1)为NSString类添加一个名称为NumberConveniencede的类别;类别名称具有唯一性,你可以向一个类中添加任意多的类别。
(2)可以指定希望向其添加类别的类(NSString),以及类别的名称(NumberConvenience),而且你还可以列出添加的方法,最后以@end结束;类别声明部分不能有实例变量部分。
实现类别
@implementation NSString (NumberConvenience)
- (NSNmuber *)lengthAsNumber{
unsigned int length = [self length];//获得字符串长度
return ([NSNumber numberWithUnsignedInt :length]);
}
@end
#import <Foundation/Foundation.h>
#import "CategoryThing.h"
//类别的作用:
//(1)将类别实现分散到多个不同文件或多个不同框架中
//(2)创建私有方法的前向引用
//(3)向对象添加非正式协议
//类别的局限性:
//(1)无法添加新的实例变量
//(2)名称冲突,如果类别和现有的方法重名,类别具有更高的优先级,解决办法,类别方法名中添加一个前缀
@interface NSString (NumberConvenience)
- (NSNumber *) lengthAsNumber;
@end
@implementation NSString (NumberConvenience)
- (NSNumber *) lengthAsNumber
{
unsigned int length= [self length];
return ([NSNumber numberWithUnsignedInt:length]);
}
@end
int main (int argc, c*****t char * argv[]) {
//我们适用类别创建的所有NSNumber类的对象将在自动释放池中被销毁一样,可变字典也将在这里被销毁
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSMutableDictionary *dict;
dict=[NSMutableDictionary dictionary];
//使用键@“hello”将整值5添加到字典中的代码如下
[dict setObject:[@"hello" lengthAsNumber] forKey: @"hello"];
[dict setObject:[@"iLikeFish" lengthAsNumber] forKey: @"iLikeFish"];
[dict setObject:[@"Once upon a time" lengthAsNumber] forKey: @"Once upon a time"];
NSLog(@"%@",dict);
CategoryThing *thing;
thing= [[CategoryThing alloc] init];
[thing setThing1:5];
[thing setThing2:23];
[thing setThing3:42];
NSLog(@"Thing are %@!",thing);
[thing release];
[pool drain];
return 0;
}
#import <Cocoa/Cocoa.h>
@interface CategoryThing : NSObject {
int thing1;
int thing2;
int thing3;
}
@end
@interface CategoryThing (Thing1)
- (void) setThing1:(int) thing1;
- (int) thing1;
@end
@interface CategoryThing (Thing2)
- (void) setThing2:(int) thing2;
- (int) thing2;
@end
@interface CategoryThing (Thing3)
- (void) setThing3:(int) thing3;
- (int) thing3;
@end
#import "CategoryThing.h"
@implementation CategoryThing
- (NSString *) description
{
NSString *desc;
desc= [NSString stringWithFormat:@"%d %d %d",
thing1,thing2,thing3];
return (desc);
}
@end
#import "CategoryThing.h"
@implementation CategoryThing (Thing1)
- (void) setThing1:(int)t1
{
thing1=t1;
}
- (int) thing1
{
return (thing1);
}
@end
#import "CategoryThing.h"
@implementation CategoryThing (Thing2)
- (void) setThing2:(int)t2
{
thing2=t2;
}
- (int) thing2
{
return (thing2);
}
#import"CategoryThing.h"
@implementation CategoryThing (Thing3)
- (void) setThing3:(int)t3
{
thing3=t3;
}
- (int) thing3
{
return(thing3);
}
@end
ios 中Category类别(扩展类)小结的更多相关文章
- [ios]objective-c中Category类别(扩展类)专题总结
本文转载至 http://yul100887.blog.163.com/blog/static/20033613520126333344127/ objective-c类别的作用?通过类别的方式, ...
- iOS中Category和Extension 原理详解
(一)Category .什么是Category? category是Objective-C .0之后添加的语言特性,别人口中的分类.类别其实都是指的category.category的主要作用是为已 ...
- DRF (Django REST framework) 中的视图扩展类
2. 五个扩展类 1)ListModelMixin 列表视图扩展类,提供list(request, *args, **kwargs)方法快速实现列表视图,返回200状态码. 该Mixin的list方法 ...
- iOS 中使用类别简化代码开发
最近需要一个函数,把CLLocation对象转化为NSDictionary,按照我以前的想法,我会写一个工具类,之后添加一个函数,类似这样 - (NSDictionary *)turnLocation ...
- iOS中的字符串扫描类NSScanner
新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置. UIColor+Hex.h文件, #import <UIKit/UIKit.h> # ...
- iOS中编写单例类的心得
单例 1.认识过的单例类有哪些: NSUserDefaults.NSNotificationCenter.NSFileManager.UIApplication 2.单例类 单例类某个类在代码编写时使 ...
- java.lang和java.lang.annotation中实现Annotation的类小结
加了注解,等于打上了某种标记,没加,则等于没有某种标记,以后,其他程序可以用反射来了解你的类上面有无何种标记,看你有什么标记,就去干相应的事.标记可以加在类,方法,字段,包上,方法的参数上. (1) ...
- 重构SP5中声明自定义扩展类为API调用
展的自定义类方法想要被前端异步调用必须在方法中 1.首先类必须继承自 AppService 类 2.方法必须指定特性 [ActionDescription("名称","编 ...
- Net中应用 Redis 扩展类
GIt地址:https://gitee.com/loogn/stackexchange-redis-typedextensions 1.stackexchange 类调用 using System; ...
随机推荐
- USACO 3.3 Shopping Offers
Shopping OffersIOI'95 In a certain shop, each kind of product has an integer price. For example, the ...
- [SQL基础教程] 3-3 HAVING
[SQL基础教程] 3-3 HAVING HAVING子句 SELECT col_1,col_2 FROM table GROUP BY col_1,col_2 HAVING col_1 = '2'; ...
- Egret 学习之 入口函数 及开始编写程序(三)
1,Egret的程序入口: C和java是以一个main函数作为入口,但egret类似于ActionScript 是以一个文档类作为入口,确切的说是以这个文档类的构造函数作为入口: 2,文档类的构造函 ...
- 窗口信息获取器 Spy4Win v0.20b 中文绿色版
软件名称:窗口信息获取器 Spy4Win v0.20b 中文绿色版软件类别:国产软件运行环境:Windows XP软件语言:简体中文授权方式:免费版软件大小:1.67 MB软件等级:整理时间:2012 ...
- juit测试中报错:org.hibernate.HibernateException: Unable to get the default Bean Validation factory
org.hibernate.HibernateException: Unable to get the default Bean Validation factory 解决方法: 解决方案: 在hib ...
- xml文件查找重复元素(超简单版)
使用WPS,新建一个表格文件,将xml拖入表格,点数据,选中存在重复项的列,点高亮重复项,OK.
- 【实验室笔记】serialport的readline()方法
在最近的小项目中,单片机中断优先级的问题,串口发送到上位机的数据有时会出现发送的数据被中断打断的问题. 于是,在上位机机上就容易出现错误,原来读取的方法是read()的方法,反复修改发送数据的格式依然 ...
- NDEF消息解析实例[转]
问题:按照NDEF消息格式来解析下列Hex串? D1 02 1F 53 70 91 01 0E 54 02 65 6E 68 65 6C 6C 6F 20 77 6F 72 6C 64 51 ...
- 寒假学干货之------LinearLayout.layout.weight
所有原始代码由这个大神写的--http://www.cnblogs.com/zhangs1986/archive/2013/01/17/2864237.html layout/activity_mai ...
- 自定义GridLayout实现条目的拖动动画特效
1.自定义GridLayout实现增加条目过度动画操作 public class MyGridLayout extends GridLayout { public MyGridLayout(Conte ...