09-Class loading process

1. Object initialization and destruction methods
  +(id) alloc; Note that alloc here is a class method. After calling the alloc method, a block of space will be allocated in memory.
and the reference count will be set to 1

  +(id) init; call the init method to initialize the object

  -(void) dealloc; Note here that dealloc is not a class method, but an instance method. dealloc method
It is used to destroy the object. When the reference count is 0, the system will automatically call the dealloc method to destroy the object.

  -(void) release; Call this method to release the reference of the object, the reference count will be -1

  -(void) retain ; call this method to increase the reference count by 1

  - (NSUInteger)retainCount; used to get how many objects are currently owned by an object

2. Five areas in memory.
The stack stores local variables.
The byte space allocated manually by the heap programmer is malloc calloc realloc function.
The BSS segment stores uninitialized global variables static variables.
The data segment (constant area) stores constant data of global static variables that have been initialized.
A code segment stores code. Stores the code of a program.
3. Class loading.
1). It is definitely necessary to access the class when creating an object.
2). Declaring a pointer variable of a class will also access the class.
When a class is accessed for the first time during program execution, the class will be stored in the code segment area in memory.
This process is called class loading.
Class loading is done only when the class is accessed for the first time.
Once the class is loaded into the code segment. It will not be released until the end of the program.
4. How are objects stored in memory?

Suppose the following is written in a function.
Person *p1 = [Person new];
1). Person *p1; will apply for a block of space in the stack memory. Declare a pointer variable p1 of type Person in the stack memory.
p1 is a pointer variable. Then only addresses can be stored.
2). [Person new]; 真正在内存中创建对象的其实是这句代码.
new做的事情
a. 在堆内存中申请1块合适大小的空间.
b. 在这个空间中根据类的模板创建对象.
类模板中定义了什么属性.就把这些属性依次的声明在对象之中.
对象中还有另外1个属性 叫做isa 是1个指针. 指向对象所属的类在代码段中的地址.
c. 初始化对象的属性
如果属性的类型是基本数据类型 那么就赋值为0
如果属性的类型是C语言的指针类型 那么就赋值为NULL
如果属性的类型是OC的类指针类型 那么就赋值为nil
d. 返回对象的地址.
3). 注意
a. 对象中只有属性,而没有方法. 自己类的属性外加1个isa指针指向代码段中的类.
b. 如何访问对象的属性
指针名->属性名;
根据指针 找到指针指向的对象 再找到对象中的属性来访问.
c. 如何调用方法.
[指针名 方法名];
先根据指针名找到对象,对象发现要调用方法 再根据对象的isa指针找到类.
然后调用类里的方法.
5.、对象的属性的默认值.
如果我们创建1个对象,没有为对象的属性赋值. 那么这个对象的属性是有值的.
如果属性的类型是基本数据类型 默认值是0
如果属性的类型是C指针类型 那么默认值是NULL
如果属性的类型是OC指针类型 那么默认值是nil




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325901710&siteId=291194637