Object基础语法Category、Extension、Protocol
来源:互联网 发布:笔记本触摸屏校准软件 编辑:程序博客网 时间:2024/05/21 07:54
可以看如下定义:
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
64-bit运行时环境和32-bit运行时环境主要有以下两点的不同:
- 数据类型的改变
- 方法调用上的改变
数据类型的改变
整型数据类型的变化如下:
那的Category的使用场景有那些呢:
1、类包含了很多个方法实现,而这些方法需要不同团队的成员来实现
2、当你在使用基础类库中的类时,你不想继承这些类而只想添加一些方法时。
Category能实现上面的需求,当然也有使用Category是需要注意的问题:
1、Category可以访问原始类的实例变量,但不能添加实例变量,如果想添加变量,那就通过继承创建子类来实现。
2、Category可以重载原始类的方法,不大不推荐这么做,这样会覆盖掉原始类的方法。如果确实要重载,那就通过继承创建子类来实现。
3、和普通接口有所区别的是,在Category的实现文件中的实例方法只要你不去调用它你可以不用实现所有声明的所有方法
MAC 开发笔记——Objective C 语法之Category和Extension
http://www.cnblogs.com/rolandash/archive/2010/03/22/1691314.html
注意Category只能用于方法,不能用于成员变量。
理解了Category,Extension就不难理解了。Extension是Category的一个特例,其名字为匿名(为空),并且新添加的方法一定要予以实现。(Category没有这个限制)
Extension:(扩展或匿名范畴)
extension & category
http://iosnote.diandian.com/post/2012-04-28/19695807Category和Extension都是用来给已定义的类增加新的内容的。
Category和原有类的耦合更低一些,声明和实现都可以写在单独的文件里。但是只能为已定义类增加Method,而不能加入instance variable。
Extension耦合比较高,声明可以单独写,但是实现必须写在原有类的@implementation中。可以增加Method和instance variable。
Extension给人感觉更像是在编写类时为了封装之类的特性而设计,和类是同时编写的。而category则是在用到某一个framework中的类时临时增加的特性。
Extension的一个特性就是可以redeclare一个instance variable,将之从readonly改为对内readwrite.
ios中的category与extension
http://www.devdiv.com/ios_category_extension-blog-1-51105.html[代码]c#/cpp/oc代码:
01
//
02
// RootViewController.h
03
// Test4
04
//
05
// Created by Vincent on 13-5-29.
06
// Copyright (c) 2013年 DevDiv Community. All rights reserved.
07
//
08
09
#import <UIKit/UIKit.h>
10
11
@
interface
RootViewController : UIViewController
12
@end
[代码]c#/cpp/oc代码:
01
//
02
// RootViewController.m
03
// Test4
04
//
05
// Created by Vincent on 13-5-29.
06
// Copyright (c) 2013年 DevDiv Community. All rights reserved.
07
//
08
09
#import "RootViewController.h"
10
11
@
interface
RootViewController ()
12
@end
13
14
@implementation RootViewController
15
16
17
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
18
{
19
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
20
if
(self) {
21
// Custom initialization
22
}
23
return
self;
24
}
25
26
- (
void
)viewDidLoad
27
{
28
[super viewDidLoad];
29
// Do any additional setup after loading the view.
30
self.title =
@"RootController"
;
31
self.navigationItem
32
}
33
34
- (
void
)didReceiveMemoryWarning
35
{
36
[super didReceiveMemoryWarning];
37
// Dispose of any resources that can be recreated.
38
}
39
40
@end
[代码]c#/cpp/oc代码:
1
@
interface
RootViewController ()
2
@end
Objective-C分类 (category)和扩展(Extension)这部分来自Blog
http://blog.csdn.net/yhawaii/article/details/6992094
- Object基础语法Category、Extension、Protocol
- OC基础之Category,Extension,Protocol
- Category、Extension、Protocol
- Category Extension protocol 总结
- 8、category、extension、protocol
- Category、Protocol、Extension
- Objective C基础(四)---Category、Extension、Protocol
- 类目 Category Extension 协议 Protocol
- Category , Extension , Protocol几个概念
- ios之Category,Extension,Protocol,delegate
- category extension protocol(类目,延展,协议)
- OC 类目category,延展extension,协议protocol
- xcode6中如何创建category,extension,protocol
- iOS创建Category Protocol Extension 文件
- OC第七天 --- NSDate Category Extension Protocol
- 类目category 扩展extension 协议protocol
- self,super,protocol,category,extension 的用法
- Objective-C——Category、Extension、Protocol
- 将一个控件的imMode设为imClose之后, 其他控件的imMode也会变为imClose
- CQU数字逻辑实验——交通灯系统设计
- 反射
- 全排列的生成方法
- 张小龙的产品设计哲学
- Object基础语法Category、Extension、Protocol
- #11 Container With Most Water
- C++标准输出流详解
- servlet3.0 异步处理
- POJ 1318 && HDU 1113 Word Amalgamation(水~)
- Android开发遇到的一些小坑洼
- tr命令和sed命令详解举例
- 查找一个字符串在另一个字符串的位置原理分析
- 【LDA】并行化LDA的一些开源资料