Effective Objective-C notes after reading (3)

21, classification attributes

  • As few attributes are used in classification, the main purpose of classification is to extend the function;
  • The data to be used in the taxonomy is defined as well as possible in the main class.

22, reference count

  • The __strong: default semantics retains this value.
  • __unsafe_unretained: does not keep this value, so it may not be safe and may have been released at the next use.
  • __weak: does not keep this value, and variables can be used safely because variables are automatically set to nil if the object is freed by the system.

23 and dealloc methods

  • If you manually manage memory, you need to call the [super dealloc] method in the dealloc method.
  • Methods try not to call the dealloc method in the other, especially the induction of other ways, because the dealloc method is the object is going to be destroyed, if this time asynchronous call method, method for end object has been released, this time may appear strange question.
  • Try not to call access to properties in the dealloc method. Avoid previous key key monitoring, lazy loading and so on.
  • In the dealloc method, all listeners are to be lifted.

24, exception handling

  • The objects in the try module must be cleaned up.
  • Security exception handling is enabled through -fobjc-arc-exceptions, but the performance of the program is reduced and the program is increased.

25, circular reference

  • Use weak to modify objects to avoid circular references.
  • Weak is automatically set to nil.

25, auto release block reduces memory peak

  • The automatic release pool is arranged in the stack, and after receiving the autorelease message, the system automatically puts it at the top.
  • The proper use of @autoreleasepool reduces memory peak.

26, about Block

  • Block writes return_type (^block_name) (parameters).
  • By default, block cannot modify the captured variables and add the __block modifier before the variable so that changes can be made inside the block.
  • Block’s memory is distributed over the stack and needs to be copied to the stack by calling copy.
  • Global block, which is distributed in global memory, does not capture the external state.
  • Use typedef to define block as much as possible.
  • Using block to solve the problem of code decentralization.
  • The block uses circular references during its use and tries not to allow users to handle circular references.