One side [Literacy]

Insert picture description here

One side [Literacy]

-(CGSize)The role of intrinsicContentSize?

1. As a read-only property of UIview in Autolayout

2. Literal translation of the inherent size, if the size of the control is not specified, it needs to be rewritten as a placeholder.

Scenario 1: The custom view in the system navigation needs to be rewritten and changed. Otherwise the size will be changed by the navigation bar.

Scenario 2: UIImageView does not specify an image and does not set its own size, then a constraint warning will be thrown back.

Write a circular reference code?

Circular references (cross-references are one of them): Both parties strongly refer to each other

Scenario 1: When designing a proxy, the declaration of the delegate needs to be modified with weak or assign. Using Strong will produce circular references.

Scenario 2: A->B->C->D->A (big ring reference)

Scenario 3: Use external attributes inside the block, not practical __weak modification

What is the difference between Assign and Weak?

Assign

  • Modification of basic data types

    • int
    • float
    • bool
  • Modify the object data type [Do not change the reference count]

  • After the modified object is released, it continues to point to its address and generates a dangling pointer (wild pointer)

    • Internal coarse leakage
    • The program crashes abnormally [EXC_BAD_ACCESS]

Weak

  • Only modify the object data type

    • Does not change the reference count
    • After the object is released, it is automatically set to nil
  • extend:

    • Why is the object set to nil after it is released?

      • 1. Execute dealloc when an object pair is destroyed

      • 2. The dealloc implementation will call a series of weak reference removal functions [ojb4 source code can be viewed]

        • dealloc
        • objc_rootDealloc
        • object_dispose
        • objc_destructInstance
        • objc_clear_deallocating
        • weak_clear_no_lock
      • 3. Look up the weak reference table according to the pointer of the current object, loop the weak references (array) corresponding to the current object and set their pointers to nil one by one

extend

  • Read and write related

    • readwrite[default]
    • readonly
  • Atomicity

    • atomic

      • Assignment and retrieval are thread-safe

      • The operation may not be [modified array]

        • Scenario 1: It is safe to assign values ​​and get elements to arrays
        • Scenario 2: It is not safe to remove and add elements to the array
    • nonatomic

  • Reference count

    • retain【ARC】

      • Rewrite the setter method of Retain modified variable under MRC?

        • -(void)setObj:(id)obj{ if (_obj != obj){[_obj release]; _obj = [obj retain]}}
        • If the source object and the passed-in object Obj are not equal, the release operation is performed on the source object, the retain operation is performed on the newly passed-in object, and then the value is assigned.
        • get on! = Judgment (to prevent the incoming object from being the original object, the original object is deleted. An error will occur in the assignment)
    • strong【MRC】

    • assign

      • Basic data type
      • Object data type
    • unsafe_unretained【MRC product】

      • Only modify the object data type
      • Not automatically set to nil
    • copy

      • Shallow copy [pointer copy]

        • 例子:@property(copy)NSMutableArray * muArray;

          • Shallow copy is a copy of memory address

          • That is, let the pointers of the copy object and the source object point to the same memory space

          • in conclusion:

            • Increase the reference count of the object being copied
            • No new memory space allocation
        • Assignment

          • MutableArray

            • copy

              • Array

                • It looks like a variable array, but adding or deleting will be abnormal
          • Array

            • copy

              • Array

                • It looks like a variable array, but adding or deleting will be abnormal
      • Deep copy

        • Contrary to shallow copy

          • The pointers of the copy object and the source object point to two pieces of memory space with the same content

          • in conclusion:

            • Does not increase the reference count of the object being copied
            • A new memory space allocation
      • Difference and connection

        • Features

          • Whether to open up new memory space
          • Whether to change the reference count of the source object
        • to sum up:

          • Copy/mutableCopy of mutable objects are deep copies

          • Immutable object

            • copy[shallow copy]
            • mutableCopy【Deep copy】
          • The copy method returns immutable objects

        • Source object type

          • copy method

            • copy object type

              • copy type [deep/light]
        • Mutable object

          • copy

            • Immutable

              • Deep copy
        • Mutable object

          • mutableCopy

            • variable

              • Deep copy
        • Immutable object

          • copy

            • Immutable

              • Shallow copy
        • Immutable object

          • mutableCopy

            • variable

              • Deep copy

Why is it necessary to set weak to strong when IB is towing?

1. The View property of UIViewController is Strong [strong ownership]

2. Add other attempts to view through IBOutlet

3. Own them by View [Is it necessary to use Weak or Strong modification here?]

Weak

  • Use when the parent view of the control and want to have the same life cycle
  • Recommend Weak, because the system will automatically manage memory for you under MRC

Strong

  • Use when the parent view of the control is destroyed and you want to continue to own the control

The relationship between UIView and UILayer?

UIView

  • Responsible for handling events (touch)
  • Participate in the response chain
  • Provide content for it

contact

  • Each UIView has a CALayer corresponding to it

  • View size and style are provided by Layer

  • Both have a tree structure

    • subViews

    • subLayers [AnchorPoint more than View]

      • presentLayer tree animation tree

        • When iOS is doing animation, the modified property value is the property value
      • modelLayer tree

        • Finally displayed on the interface
      • render tree

  • Control wants to repaint

    • [UIView setNeedDispaly]

      • CALayer setNeedDisplay] [Method of the same name]

        • [CALayer dispaly]

          • Proxy response

            • Asynchronous drawing entry
          • Agent does not respond

            • System drawing process
      • There is no real drawing at this time, and the current RunLoop needs to end

CALayer

  • Responsible for displaying content
  • Modify properties, support implicit animation

End

XMind - Trial Version

Guess you like

Origin blog.csdn.net/u010436133/article/details/109005887