1. Memory Management
-
- Heap and Stack
- Stack (operating System): automatically allocated by the operating system release, storing the function parameter values (parameters), the value of local variables and so on. It works like a stack in a data structure (advanced back-out)
- Heap (operating system): Usually released by the programmer, if the programmer does not release, the program ends may be recycled by the OS, distributed in a manner similar to the list
- As long as Alloc Init was created, it's all in the heap.
- The OC object is stored in the heap, the data system in the heap is not automatically released and needs to be released manually.
- Non-OC objects are generally placed in the stack, and the stack memory is automatically reclaimed by the system.
-
- Memory leak---if an OC object occupies a piece of memory, but is not used. So this is a memory leak.
-
- The role of the reference counter--the reference counter indicates how many people are using this object
-
- Actions that reference counters
- object is created, the reference counter is initially 1
- Sends an retain message to the object, and the reference counter is +1
- Send a release message to the object, reference counter-1
- Send Retaincount to the object, you can get the value of the current reference counter (the obtained value is not accurate)
-
- Dealloc
- When an object reference counter is 0, the system automatically sends an DEALLOC message to the object. (So there is no call from the Dealloc method to know if the object is destroyed)
- The general will dealloc method rewrite, here release the related resources, Dealloc is the object's last words
- Once you override the Dealloc method, you must call [Super Dealloc] and put it in the last call
- The Dealloc method cannot be called directly
- Once the object is recycled, the memory it uses is no longer available, and sticking to it can cause the program to crash (wild pointer error)
-
- Wild hands and null pointers
- Wild Hands
- As soon as an object is released, we call this object "zombie object."
- When a pointer points to a zombie object, we call this pointer a wild pointer
- If you send a message to a wild pointer, you'll get an error.
- [Person release]: message sent to deallocated instance 0x1001146b0
- Null pointer
- In order to avoid sending a message to the wild pointer error, in general, when an object is freed, we will set the object's pointer to a null pointer
- Because sending a message to the null pointer in OC is not an error.
-
- ARC and MRC
- Arc:automatic (Auto) Reference (Reference) counting (count)
- What is automatic reference counting?
- Without the programmer to manage the content, the compiler will automatically add Release/retain and other code to us where appropriate.
- Note: The arc in OC is not the same as the garbage collection mechanism in Java, garbage collection in Java is the system dry, and the arc in OC is the compiler doing
- Mrc:manul (manual) Reference (Reference) counting (count)
- What is a manual reference count?
- The contents of all objects need to be managed manually, requiring programmers to write their own release/retain and other code
-
- The principle of memory management---there is a reduction
- For example: Once alloc corresponds to the release, once the retain corresponds once Relese
-
- Multi-Object Memory management
- When a object wants to use a B object, it must make a retain to the B object, in order to guarantee the existence of the B object, which means that the B object can be used whenever a object is
- When the A object is released, be sure to release the B object once so that the A object is released, and the B object is freed to avoid memory leaks
- To sum up: there is a decrease in growth
3. Property modifier
- Property modifiers of the same type cannot be used together
- Different types of property modifiers can be used in conjunction with multiple, separated by numbers
- As long as you write the property in iOS development, write the nonatomic immediately.
- Multithreading
- Atomic: Low performance (default)
- Nonatiomic: High Performance
- In iOS development 99.99% is written nonatomic
Readonly only generates getter methods
Readwrite: will generate a getter and generate a setter, the default is to write nothing is readwrite
Getter: can give the generated getter method a name
Setter: can give the generated setter method a name
Retain: will automatically help us generate code for getter/setter method memory management
Assign: will not help us generate the memory of the set method memory management, only generate ordinary getter / setter methods, the default is nothing to write is assign
4. @class
- Improve compilation efficiency
#import"Car.h"
- Since import is a precompiled directive, he will copy the files in "" to the location where import is located.
- and import has a feature, as long as the file in "" changes, then import will be copied again (update operation)
@class Car;
@class just tells the compiler that the name after @class is a class and won't do any copy operations.
Note: Since @class simply tells the name of the compiler to be a class, the compiler does not know which properties and methods are in the class, so you need to import this class when using this class in .m.
to sum up :
1. If both are imported in .h, if A copies B, B copies C. If C is modified, then both B and A need to be re-copied. Because C is modified then B will be copied again, and B will be re-created. After copying, the equivalent of B is also modified, then A also needs to be re-copied. That is to say, if all are copied in .h, they will be copied again if there is an indirect relationship.
2. If you use @class in .h and import in .m, then if a file changes, only the file that has a direct relationship with this file will be copied again.
3. So use @class in .h to improve compilation efficiency
- Mutual copying becomes feasible
- If two classes are copied to each other, such as a copy B, b copy A, this will cause an error.
- How to FIX: Use @class in. h, import in. m
- Because if you use import in. h, then a copy B, b and copy a, will form a dead loop.
- If you use @class in. h, then no copy operation is done, and the import in. M only copies the corresponding file, and does not form a dead loop
5. Cyclic retain
- If a pair is to have a B object and B corresponds to have a object, then a cyclic retain is formed
- How to solve this problem: do not let a retain B, b retain a
- Let one side do not do retain operation can
6. Autorelease's Precautions
- You can create multiple auto-release pools in one program, and automatically release pools can also be nested
- If there are multiple auto-free pools, the auto-free pool is stored as a "stack"--the characteristics of the stack: Advanced back
- Never write multiple auturelease
- A alloc/new corresponds to a autorelease or release
- If you write Autorelease, don't write release.
7.ARC
- ARC's criteria: The object is freed as long as no strong pointer is directed to the object
- By default, all pointers are strong pointers
__strong Person *p = [[Person alloc]init]; //This is created by a strong pointer
__weak Person *p = [[Person alloc]init]; //This is created by a weak pointer and will be released immediately
- In Arc, if you save the object, do not use assign, use weak---assign is dedicated to saving the basic data type. Save objects to use weak
- The difference between arc and MRC
- MRC, a object that wants to have a B object, needs a retain for the B object
- A object does not have a B object, and a release for the B object is required
- The property when the retain, Dealloc time to release
- ARC, a object that wants to have a B object, needs a strong pointer to the B object
- A object does not use a B object, nothing is done. The compiler automatically helps me do
- Save an object in arc with strong, equivalent to the retain in MRC
8. Classification
- Basic concepts
- You can only add a method to a category, but you cannot add a variable
- Writing
@interface ClassName (CategoryName) NewMethod;
1. Name of the ClassName classification
2. CategoryName expansion method
3. NewMethod expansion method
- Anonymous classification--(also known as "extended" "Class extensions")
-Function---you can extend some private member variables and methods for a class
-In fact, the anonymous classification is in the. m file, written in the @interface Class name () @end
-Accumulate your own classification controls for easy accumulation to improve development efficiency
- Precautions
- Classification is used to add methods to an existing class, it can only add methods, cannot add attributes (member variables),
- @property in the classification, only the declaration of the Setter/getter method is generated, and the implementation and the private member variable are not generated
- You can access the properties of the existing class in the taxonomy. h
- method is called by the
- Classification
- This class
- Parent class
- Attention
- If there is a method in the classification that has the same name as the original, the method in the classification is called (ignoring the method of the original class) ——— recommend not to write that way.
- If multiple classifications have a method of the same name as the original, then who executes the method when it is called, which is determined by the compiler (the last method that participates in the compiled classification is executed)
9.Block
- Definition--block is a special type of data in iOS, similar to "point to function"
- Application Scenario--When the code is found to be the same as before and after, this time can be used block
- Animation
- Multithreading
- Collection traversal
- Network Request callback
- Role
- Used to save a piece of code that can be removed again at the appropriate time to invoke
- Functions similar to functions and methods
- Format
- return value type (^block variable name) (parameter list) = ^ (formal parameter list) {code snippet};
- For example
Void(^roseBlock)();
1. () represents that the code saved in the future will have no formal parameters.
2. (^roseBlock) means that roseBlock is a block variable that can be used to save a block of code.
3. If the block has no parameters, then the () after ^ can be omitted.
E.g:
Int multiplier = 7;
Int (^myBlock)(int) = ^(int num){ return num *multiplier ;};
1. The ^ symbol declares myBlock as a block object
2. myBlock is a block object, returning an integer value
3. (int) represents a parameter, the type of the parameter is also an integer value
4. ^(int num) The name of the parameter is num
5. { return num *multiplier ;} This is the main part of the block object
6. ^(int num){ return num *multiplier ;} This is the grammatical structure that defines the block object. This part is the value assigned to the myBlock variable.
Precautions for Block
-
- Block and typedef
- Note: The name of the block variable is an alias, like a pointer to a function, using a typedef to give the block its alias.
- It is recommended to define aliases with TypeDef, and it is easier to use aliases in coding.
-
- Precautions for Block
- The outside variables can be accessed in the block
- A variable with the same name can be defined in a block, and if a variable with the same name is defined in the block, the variable in the block is accessed in the block
- By default, you cannot modify the value of an external variable in a block
- Because the variables in block and outside variables are not the same variable
- If an external variable is accessed in the block, the block copies the external variables into the heap memory
- Because the external variables used in the block are copy, modifying the values of the external variables before the call does not affect the copy value in the Block
- If you want to modify the value of an external variable in a block, you must precede the external variable with __block
- If you modify the value of an external variable in a block, it affects the value of the external variable.
- Whether the block is stored in a heap or in a stack
- Block is stored in the stack by default, and if a copy operation is made to the block, the block is transferred to the heap
- If block is in the stack and the object is accessed from the block, then the object is not retain.
- But if the block is in the heap and the block accesses the outside object, then the outside object will be retain once.
- If the object in the block is accessed, be sure to add __block to the object, as long as the __block, even if the block is in the heap, will not be retain to the outside object.
- If it's in ARC development, you need to add __weak to the front.
Foundation = Objective-c _ Part4