Define Class

  • Inheritance relationships
  • Property (@Property)
    • Ivar (Member variable)
    • getter & setter
    • Use of the copy, strong, assign, and weak modifiers
  • Function (method)
    • selector
    • Class and member functions

Category and Extension

  • Category

    • Ivar cannot be added

      Categories declaring attributes using @property do not automatically generate member variables.Copy the code
    • Enhanced class functionality

    • Separated class implementation

  • Extension

    • Increase ivar
    • It is used to separate interfaces
    • private

Protocol

  • Protocols Define Messaging Contracts
  • Similar to Java Inerface, it describes a set of related method definitions
  • The open and close principle abstracts the function of the class and hides the concrete implementation of the class
  • Protocols can be inherited from other protocols
  • Protocol constraints are weak and tend to only generate warnings, not affect the final compilation
@protocal DemoProtocal <NSObject> @optional - (void)optionalMethod; // Implementers can not implement without warning @required - (void)requiredMethod; @end@interface Demo: NSObject<DemoProtocal> @property(nonatomic, weak) id<DemoProtocal> delegate; @end @implementation -(void)requiredMethod{ if([delegate respondsToSelector:@selector(requiredMethod)]){ [delegate requiredMethod]; } } @endCopy the code

Method Dispatch

  • Function overload?

    • No, the return value and parameter type of the function do not affect the resolution of the function, compilation error.
  • Function override

    • OK
  • Does a subclass have a method of the same name as its parent? (Different return value and parameter types)

    • Dynamic resolution, which calls the subclass implementation
    • Force a static call to a parent method, possibly crash
  • selector vs method

    • Selector is a char star
    • Method addresses are function addresses
  • objc_msgSend

    • OC messaging
    • The dynamic analysis
  • method lookup

    • Inheritance chain
  • Type encoding

    • Method type encoding (type of parameter and return value)

Memory Management

ARC

  • Reference counting and MRC

    • Manual call retain/release/autorelease
    • Memory management is difficult, prone to memory leaks and wild Pointers
    • What is ARC? Automatic drinking count
      • The compiler generates retain/release
      • Zeroing Weak Pointer (return to 0, automatically set to nil)
      • Object reference diagrams are still needed to avoid memory leaks from reference rings
  • Strong, weak, assign

    • Ownership and object reference graph
    • Best practices
      • Base types use assign
      • The delegate with weak
      • NSArray, NSDictionary, NSString, block use copy
      • Other use strong
      • In the block, Self breaks the loop with weak to drink
    • Avoid referencing rings a-strong -> B-strong -> c-strong -> A
    • Memory graph debugger
  • Autoreleasepool

  • Cycle to drink

    Objective-c Advanced Programming for iOS and OS X Multithreading and Memory Management

RunTime

  • Structure of classes and objects at run time
  • The ability of the runtime to access and modify the class structure
  • Apple Source Browser
  • Practice adding a new class with Runtime

thinking

  1. Why do properties of NSString, NSArray, and NSDictionary use deep and shallow copies of the copy set?
  2. How to do selector not found Crash protection?