分类
分类(category)是OBJ-C语法中非常方便的一部分,可以在运行时向某个现有类添加新的方法。包括Cocoa框架中的任何类,我们都可以对其进行扩展,这是从Smalltalk(oc的鼻祖)继承过来的,跟C#中的扩展方法比较像。
不谈历史,先来看下用分类进行私有成员的表现。
#import "ViewController.h"
@interface ViewController ()
{
UIButton *button1;
}
@end
比如这里在.m文件,ViewController的上面有一个匿名的分类(类的扩展),这里声明的变量都属于我们的“私有成员”,对外属于隐藏状态。
这样,我们可以进行更多元化的定义,比如“公有可读,私有读写”。
.h文件中声明一个状态属性
@property (nonatomic, readonly) BOOL viewState;
然后在.m文件的匿名分类中对其声明为readwrite, 便可以进行读写操作了。
@interface ViewController ()
{
UIButton *button1;
}
@property (nonatomic, readwrite) BOOL viewState;
@end
@implementation ViewController
- (void)setViewState:(BOOL)viewState
{
code...
return ;
}
- (BOOL)viewState
{
code...
return ;
}
于是,对属性信息的封装便完成了。
添加“成员”的动态实现
@interface Person (Address)
@property (nonatomic, strong) NSString *address;
@end
这样,我就必须对其进行设值取值了,嗯,但是它只是一个分类声明的属性,并不是成员。
- (NSString *)address
{
return self.address;
}
= =于是,我们找到了更好的方法,就是利用runtime的关联引用
static char KPersonAddressKey;
//setter
- (void)setAddress:(NSString *)address
{
objc_setAssociatedObject(self, &KPersonAddressKey, address, OBJC_ASSOCIATION_COPY);
}
//getter
- (NSString *)address
{
return objc_getAssociatedObject(self, &KPersonAddressKey);
}
下面对其进行了测试
Person *p = [[Person alloc] init];
p.address = @"Tianjin University";
NSLog(@"%@", p.address);
打印结果
2825



被折叠的 条评论
为什么被折叠?



