通常情况下,我们用
【类别】
去对【原有类】
进行【属性】
的添加,如果在编译
的时候不会报错,但是在运行阶段
则会崩溃!
所以,我们通过对属性的set、get
方法重写并调用【关联属性】
的方法,即可完成属性的【赋值 和 打印】
,并且不会崩溃。
static const char delegateKey //这是用来确认唯一身份的标识
■ 设置类关联属性
objc_setAssociatedObject (object , key , value , policy)
■ 获取类关联属性
objc_getAssociatedObject (object , key)
object
:被关联的对象(那个对象需要关联)
key
:map表里key的一个唯一区分标志
value
:属性
policy
:属性以什么方式去保存(copy、strong、assign、retain等)
student.h
#import
@interface CFStudent : NSObject
@property(nonatomic,copy)NSString *name;
//吃东西
- (void)eating;
@end
student.m
#import "CFStudent.h"
#import "CFStudent+Extension.h"
/*
*扩展(私有:变量、属性)
*/
@interface CFStudent()
{
NSInteger _num; //学号
}
@property(nonatomic,assign)BOOL sex;
@end
@implementation CFStudent
//吃东西
- (void)eating
{
_num = 123456;
self.sex = YES;
_class = @(2018);
NSLog(@"_num:%ld self.sex:%d _class:%@",(long)_num,self.sex,_class);
NSLog(@"Person eating!");
}
@end
CFStudent+Category.h
#import "CFStudent.h"
@protocol ExaminationRul
@optional
- (void)level6test;
@end
/*
*分类(实例属性、类属性、实例方法、类方法、)
*/
@interface CFStudent (Category)
//注意:这些属性都使用不了,因为分类不能生成变量(内存已分配好),所以也没有setter、getter方法
@property(nonatomic,copy)NSString *age;
@property(class,nonatomic,copy)NSString *teacher; //类属性使多个实例可以共享变量
@property(nonatomic,weak)id delegate;
//玩耍
- (void)playing;
//睡觉
+ (void)sleeping;
@end
CFStudent+Category.m
#import "CFStudent+Category.h"
#import
@implementation CFStudent (Category)
static const char ageKey;
static const char teacherKey;
static const char delegateKey;
- (void)setAge:(NSString *)age
{
objc_setAssociatedObject(self, &ageKey, age, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (NSString *)age
{
NSString *age = objc_getAssociatedObject(self, &ageKey);
return age;
}
+ (void)setTeacher:(NSString *)teacher
{
objc_setAssociatedObject(self, &teacherKey, teacher, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
+ (NSString *)teacher
{
//注意:这里的self是CFStudent
//而实例方法里的self是student
NSString *teacher = objc_getAssociatedObject(self, &teacherKey);
return teacher;
}
- (void)setDelegate:(id)delegate
{
objc_setAssociatedObject(self, &delegateKey, delegate, 0);
}
- (id)delegate
{
id delegate = objc_getAssociatedObject(self, &delegateKey);
return delegate;
}
//玩耍
- (void)playing
{
NSLog(@"Student playing!");
}
//睡觉
+ (void)sleeping
{
NSLog(@"Student sleeping!");
}
- (void)level6test{
NSLog(@"ExaminationRul-level6test!");
}
@end
CFStudent+Extension.h
#import "CFStudent.h"
@interface CFStudent ()
{
NSNumber *_class; //班级
}
@end
.main
#import
#import "CFStudent.h"
#import "CFStudent+Category.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
CFStudent *student = [[CFStudent alloc]init];
/*
*1.属性
*/
student.name = @"lilei";
NSLog(@"student.name:%@",student.name);
student.age = @"20";
NSLog(@"student.age:%@",student.age);
CFStudent.teacher = @"zhangsan";
NSLog(@"CFStudent:%@",CFStudent.teacher);
/*
*2.方法
*/
[student eating];
[student playing];
[CFStudent sleeping];
/*
*3.协议
*/
student.delegate = student;
[student.delegate level6test];
}
return 0;
}
打印结果:
2019-03-13 14:10:13.458535+0800 Category Associat的介绍[13462:1406592] student.name:lilei
2019-03-13 14:10:13.458766+0800 Category Associat的介绍[13462:1406592] student.age:20
2019-03-13 14:10:13.458803+0800 Category Associat的介绍[13462:1406592] wdwname:sdf
2019-03-13 14:10:13.458864+0800 Category Associat的介绍[13462:1406592] CFStudent:zhangsan
2019-03-13 14:10:13.458917+0800 Category Associat的介绍[13462:1406592] _num:123456 self.sex:1 _class:2018
2019-03-13 14:10:13.458945+0800 Category Associat的介绍[13462:1406592] Person eating!
2019-03-13 14:10:13.458961+0800 Category Associat的介绍[13462:1406592] Student playing!
2019-03-13 14:10:13.458979+0800 Category Associat的介绍[13462:1406592] Student sleeping!
2019-03-13 14:10:13.458998+0800 Category Associat的介绍[13462:1406592] ExaminationRul-level6test!
Program ended with exit code: 0