了解对象的本质
我们先看下面的代码
@interface XDPerson : NSObject
{
NSString *nickName; //成员变量
// UILabel *label; //实例变量 是特殊的成员变量 通过类实例出来的
}
@property (nonatomic,copy) NSString *name; //属性
@end
@implementation XDPerson
@end
clang -rewrite-objc main.m -o main.cpp 将文件编译成底层的.cpp文件。
我们可以在main.cpp的文件里面看到一些这样的代码
struct NSObject_IMPL {
Class isa;
};
···
#ifndef _REWRITER_typedef_XDPerson
#define _REWRITER_typedef_XDPerson
typedef struct objc_object XDPerson;
typedef struct {} _objc_exc_XDPerson;
#endif
extern "C" unsigned long OBJC_IVAR_$_XDPerson$_name;
struct XDPerson_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSString *nickName;
NSString *_name;
};
// @property (nonatomic,copy) NSString *name;
/* @end */
// @implementation XDPerson
static NSString * _I_XDPerson_name(XDPerson * self, SEL _cmd) { return (*(NSString **)((char *)self + OBJC_IVAR_$_XDPerson$_name)); }
extern "C" __declspec(dllimport) void objc_setProperty (id, SEL, long, id, bool, bool);
static void _I_XDPerson_setName_(XDPerson * self, SEL _cmd, NSString *name) { objc_setProperty (self, _cmd, __OFFSETOFIVAR__(struct XDPerson, _name), (id)name, 0, 1); }
// @end
看XDPerson_IMPL结构体内有三个成员变量
- isa 来自父类继承;
- nickName;
- _name;
对于属性name在底层系统给我们编译了三样东西:
- 成员变量_name;
- getter方法 ,static NSString * _I_XDPerson_name(XDPerson * self, SEL _cmd)看到了默认带的两个参数,cls,SEL;
- setter方法,static void I_XDPerson_setName(XDPerson * self, SEL _cmd, NSString *name) 看到了默认带的两个参数+第三个参数,cls,SEL,name。这里setter方法里面的objc_setProperty是比较重要的。
而我们的成员变量依旧只是一个成员变量。
有此我们可以推出一个结论:
1. 对象的本质在底层就是一个结构体XDPerson_IMPL;
2. 属性与成员变量之间的区别,属性是又 成员变量 +getter方法+setter方法;
看.cpp文件里面method_list_t
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[2];
} _OBJC_$_INSTANCE_METHODS_XDPerson __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
2,
{{(struct objc_selector *)"name", "@16@0:8", (void *)_I_XDPerson_name},
{(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_XDPerson_setName_}}
};
{{(struct objc_selector *)"name", "@16@0:8", (void *)_I_XDPerson_name},
{(struct objc_selector *)"setName:", "v24@0:8@16", (void *)I_XDPerson_setName}}
这个地方解释一下SEL->selector,Type->方法前面,Imp->方法实现。
其中对方法签名做一下解释:
@ 返回类型 id ,16返回的长度,
@ 参数类型id,0-7,
: 参数类型SEL,8-15;
NSLog(@"char--%s",@encode(char));
NSLog(@"short--%s",@encode(short));
NSLog(@"int--%s",@encode(int));
NSLog(@"long--%s",@encode(long));
NSLog(@"long long--%s",@encode(long long));
NSLog(@"unsigned char--%s",@encode(unsigned char));
NSLog(@"unsigned short--%s",@encode(unsigned short));
NSLog(@"unsigned int--%s",@encode(unsigned int));
NSLog(@"unsigned long--%s",@encode(unsigned long));
NSLog(@"float--%s",@encode(float));
NSLog(@"BOOL--%s",@encode(BOOL));
NSLog(@"void--%s",@encode(void));
NSLog(@"char *--%s",@encode(char *));
NSLog(@"id--%s",@encode(id));
NSLog(@"Class--%s",@encode(Class));
NSLog(@"SEL--%s",@encode(SEL));
int array[] = {1,2};
NSLog(@"int[]--%s",@encode(typeof(array)));
typedef struct person{
int age;
NSString *name;
}Person;
NSLog(@"struct--%s",@encode(Person));
typedef union teacher{
char *a;
BOOL b;
}Teacher;
NSLog(@"union--%s",@encode(Teacher));
2019-12-19 20:47:51.143558+0800 Object本质[2623:63103] char--c
2019-12-19 20:47:51.143887+0800 Object本质[2623:63103] short--s
2019-12-19 20:47:51.143906+0800 Object本质[2623:63103] int--i
2019-12-19 20:47:51.143966+0800 Object本质[2623:63103] long--q
2019-12-19 20:47:51.144035+0800 Object本质[2623:63103] long long--q
2019-12-19 20:47:51.144080+0800 Object本质[2623:63103] unsigned char--C
2019-12-19 20:47:51.144108+0800 Object本质[2623:63103] unsigned short--S
2019-12-19 20:47:51.144131+0800 Object本质[2623:63103] unsigned int--I
2019-12-19 20:47:51.144153+0800 Object本质[2623:63103] unsigned long--Q
2019-12-19 20:47:51.144173+0800 Object本质[2623:63103] float--f
2019-12-19 20:47:51.144193+0800 Object本质[2623:63103] BOOL--c
2019-12-19 20:47:51.144213+0800 Object本质[2623:63103] void--v
2019-12-19 20:47:51.144234+0800 Object本质[2623:63103] char *--*
2019-12-19 20:47:51.144256+0800 Object本质[2623:63103] id--@
2019-12-19 20:47:51.144277+0800 Object本质[2623:63103] Class--#
2019-12-19 20:47:51.144297+0800 Object本质[2623:63103] SEL--:
2019-12-19 20:47:51.144317+0800 Object本质[2623:63103] int[]--[2i]
2019-12-19 20:47:51.144337+0800 Object本质[2623:63103] struct--{person=i@}
2019-12-19 20:47:51.144359+0800 Object本质[2623:63103] union--(teacher=*c)
查看打印结果可以看到对应的typeEncode
当然我们可以从苹果官方文档可以看到TypeEncode或者Xcode 在@encode commod+shift+0 进入官方文档直接查看
ViewDidLoad编译
上面我们是在mac的情形下面编译出来的结构,现在我们在ViewDidLoad编译一下会出现什么呢?
首先提供编译指令:
xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m
xcrun -sdk iphoneos clang -rewrite-objc ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end
查看cpp文件
#ifndef _REWRITER_typedef_ViewController
#define _REWRITER_typedef_ViewController
typedef struct objc_object ViewController;
typedef struct {} _objc_exc_ViewController;
#endif
struct ViewController_IMPL {
struct UIViewController_IMPL UIViewController_IVARS;
};
/* @end */
// @interface ViewController ()
/* @end */
// @implementation ViewController
static void _I_ViewController_viewDidLoad(ViewController * self, SEL _cmd) {
((void (*)(__rw_objc_super *, SEL))(void *)objc_msgSendSuper)((__rw_objc_super){(id)self, (id)class_getSuperclass(objc_getClass("ViewController"))}, sel_registerName("viewDidLoad"));
}
struct UIViewController_IMPL {
struct UIResponder_IMPL UIResponder_IVARS;
};
// @end
我们的类ViewController在底层也被编译成了 ViewController_IMPL 结构体 继承了父类UIVIewController的属性;
同时看_I_ViewController_viewDidLoad有默认的两个参数 self,_cmd。
(objc_msgSendSuper)({self, class_getSuperclass(objc_getClass("ViewController"))}, sel_registerName("viewDidLoad"));
精简一下代码,就可以看到[super ViewDidLoad],
其本质上就是给父类发送消息objc_msgSendSuper(self.super,"viewDidLoad");
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[1];
} _OBJC_$_INSTANCE_METHODS_ViewController __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
1,
{{(struct objc_selector *)"viewDidLoad", "v16@0:8", (void *)_I_ViewController_viewDidLoad}}
};
同样的在_method_list_t中也可以看到SEL,type,imp。
对象与类在内存中的数量
XDPerson *p1 = [XDPerson alloc];
XDPerson *p2 = [XDPerson alloc];
XDPerson *p3 = [XDPerson alloc];
NSLog(@"p1-%p",p1);
NSLog(@"p2-%p",p2);
NSLog(@"p3-%p",p3);
Class c1 = [XDPerson class];
Class c2 = [XDPerson alloc].class;
Class c3 = object_getClass([XDPerson alloc]);
NSLog(@"c1-%p",c1);
NSLog(@"c2-%p",c2);
NSLog(@"c3-%p",c3);
2019-12-19 21:42:33.901531+0800 alloc探索[3108:105948] p1-0x600000639da0
2019-12-19 21:42:33.901750+0800 alloc探索[3108:105948] p2-0x60000063ae20
2019-12-19 21:42:33.901878+0800 alloc探索[3108:105948] p3-0x600000639620
2019-12-19 21:42:33.901996+0800 alloc探索[3108:105948] c1-0x10b8f30e8
2019-12-19 21:42:33.902107+0800 alloc探索[3108:105948] c2-0x10b8f30e8
2019-12-19 21:42:33.902205+0800 alloc探索[3108:105948] c3-0x10b8f30e8
从打印结果可以看出来,我们的对象有多个,我们的类只有一个。
说明对象alloc就是一个新的对象,占用一份新的内存。而类在系统内存中就只有一份。