@property 的本質(zhì)是什么?
@property = ivar + getter + setter;
實(shí)例變量+get方法+set方法,也就是說使用@property 系統(tǒng)會自動生成setter和getter方法;
一丶驗(yàn)證:
添加一個屬性
@interface Research001 ()
@property (nonatomic, copy) NSString *name;
@end
利用class_copyPropertyList 查看類的所有屬性
unsigned int count;
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (unsigned int i = 0; i< count; i++)
{
const char *name = property_getName(propertyList[i]);
NSLog(@"__%@",[NSString stringWithUTF8String:name]);
objc_property_t property = propertyList[i];
const char *a = property_getAttributes(property);
NSLog(@"屬性信息__%@",[NSString stringWithUTF8String:a]);
}
結(jié)果:
__name
屬性信息__T@"NSString",C,N,V_name
再利用class_copyMethodList查看方法列表
u_int methodCount;
NSMutableArray *methodList = [NSMutableArray array];
Method *methods = class_copyMethodList([self class], &methodCount);
for (int i = 0; i < methodCount; i++)
{
SEL name = method_getName(methods[i]);
NSString *strName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
[methodList addObject:strName];
}
free(methods);
NSLog(@"方法列表:%@",methodList);
結(jié)果:
方法列表: (
".cxx_destruct",
"name",
"viewDidLoad",
"setName:",
)
二丶分析
屬性信息__T@"NSString",C,N,V_name
這一堆信息是什么?
翻閱Objective-C Runtime Programming Guide

Paste_Image.png
得出:
//T@"NSString",C,N,V_name
//T 類型
//C copy
//N nonatomic
//V 實(shí)例變量
"name",
"setName:",
可以從方法列表中,得出生成了set/get方法;
三丶runtime 實(shí)現(xiàn)
思路利用runtime.h文件的2個函數(shù)完成操作;
/**
* Adds a property to a class.
*
* @param cls 修改的類
* @param name 屬性名字
* @param attributes 屬性數(shù)組
* @param attributeCount 屬性數(shù)組數(shù)量
* @return y 成功,n失敗
*/
OBJC_EXPORT BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount)
OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0);
/**
* Adds a new method to a class with a given name and implementation.
*
* @param cls The class to which to add a method.//添加方法名字
* @param name A selector that specifies the name of the method being added.//方法名稱
* @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.//方法的實(shí)現(xiàn)必須至少2個參數(shù),self 和 _cmd
* @param types An array of characters that describe the types of the arguments to the method. //描述
*
* @return YES if the method was added successfully, otherwise NO //y成功,n失敗
* (for example, the class already contains a method implementation with that name).
*
* @note class_addMethod will add an override of a superclass's implementation, //會覆蓋superclass 的實(shí)現(xiàn);
* but will not replace an existing implementation in this class. //已經(jīng)存在的不會替換
* To change an existing implementation, use method_setImplementation.//想要改變,使用method_setImplementation方法
*/
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp,
const char *types)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
我們要生成一個myName的屬性;結(jié)果要跟@property 一樣;
第一步:生成屬性
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "" }; // C = copy
objc_property_attribute_t nonatomic = { "N", "" }; //nonatomic
objc_property_attribute_t backingivar = { "V", "_myName" };//V 實(shí)例變量
objc_property_attribute_t attrs[] = { type, ownership,nonatomic, backingivar };
class_addProperty([self class], "myName", attrs, 4);
第二步:生成方法:
NSString *myNameGetter(id self, SEL _cmd) {
Ivar ivar = class_getInstanceVariable([self class], "_privateMyName");
return object_getIvar(self, ivar);
}
void myNameSetter(id self, SEL _cmd, NSString *newName) {
Ivar ivar = class_getInstanceVariable([self class], "_privateMyName");
id oldName = object_getIvar(self, ivar);
if (oldName != newName) object_setIvar(self, ivar, [newName copy]);
}
//其中 “v@:” 表示返回值和參數(shù)
if(class_addMethod([self class], NSSelectorFromString(@"myName"), (IMP)myNameGetter, "@@:"))
{
NSLog(@"myName get 方法添加成功");
}
else
{
NSLog(@"myName get 方法添加失敗");
}
if(class_addMethod([self class], NSSelectorFromString(@"setMyName:"), (IMP)myNameSetter, "v@:@"))
{
NSLog(@"myName set 方法添加成功");
}
else
{
NSLog(@"myName set 方法添加失敗");
}
打印結(jié)果:
方法列表: (
"setMyName:",
"myName",
"touchesBegan:withEvent:",
"viewDidLoad",
".cxx_destruct",
"setName:",
"name",
!!!cxx_destruct 是什么東西???哪來的
四丶擴(kuò)展
cxx_destruct 字面意思是自毀方法?
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//偷偷調(diào)用一下
NSLog(@"%@",[self performSelector:NSSelectorFromString(@".cxx_destruct")]); ;
}
都是空的;
ZBResearchDemo[9629:166638] (null)
ZBResearchDemo[9629:166638] (null)
ZBResearchDemo[9629:166638] (null)
根據(jù)這篇博客:http://blog.jobbole.com/65028/
可以窺探一二
.cxx_destruct方法原本是為了C++對象析構(gòu)的,ARC借用了這個方法插入代碼實(shí)現(xiàn)了自動內(nèi)存釋放的工作
1.只有在ARC下這個方法才會出現(xiàn)(試驗(yàn)代碼的情況下)
2.只有當(dāng)前類擁有實(shí)例變量時(不論是不是用property)這個方法才會出現(xiàn),且父類的實(shí)例變量不會導(dǎo)致子類擁有這個方法
3.出現(xiàn)這個方法和變量是否被賦值,賦值成什么沒有關(guān)系