The keyword

  • Strong: In ARC, as long as an object is pointed to by a strong pointer, it will not be destroyed. If the object is not pointed to by any strong pointer, it is destroyed. By default, all instance variables and local variables are of type strong. It is safe to say that a pointer of type strong behaves similarly to a retained pointer of type non-ARC

  • What weak does: In ARC, any weak pointer to this object will be set to nil. This T feature is very useful, I believe many developers have been confused by the EXC_BAD_ACCESS caused by Pointers to released objects. After using ARC, no pointer of strong or weak type will point to a destroyed object.

  • Assign simple values to the base data types (NSInteger, CGFloat, double, char, float, etc.) without changing the reference count. Assign is used for non-pointer variables. For basic data types (such as NSInteger) and C data types (int, float, double, char, etc.), in addition to ids

Such as:

@property (nonatomic, assign) int number; @property (nonatomic, assign) id className; // The id must be assigned. Remember: if you don't need an *, use assignCopy the code
  • Copy, setter method copy, same as retain process, old value release, copy new object, retainCount is 1. This is a mechanism introduced to reduce dependency on context. Copy is used when you don’t want a and B to share a piece of memory. A and B each have their own memory.

  • Nonatomic access, no synchronization, multi-threaded concurrent access improves performance. Note that without this attribute, both access methods are atomic transaction access by default. The lock is added to the instance level of the owning object. .

Atomic and nonatomic are used to determine whether getters and setters generated by the compiler are atomic operations. In a multithreaded environment, atomic manipulation is necessary, otherwise error results may be caused.

  • retainAnd:assignIn contrast, to solve the problem of an object being freed after being referenced by another object, we use a retain declaration. A retained object will retain its reference count (+1) every time it is referenced, and will retain the reference count (-1) every time it is released. Even if the object itself is released, it will retain the reference count as long as other objects reference itdeallocThe destructor reclaims memory.

Weak and strong

Weak differs from strong in that an object is released when it no longer has a strong pointer to it, even if it still has an weak pointer to it. Once the last strong pointer is gone, the object is released and any remaining weak Pointers are cleared.

Weak and assign:

Weak only modifies objects;

Weak does not cause wild pointer problems. Because when the weak modified object is released (the reference counter is 0), the pointer is automatically set to nil and messages to the object later will not crash. Weak is safe. Assign modifies objects, and primitive data types; Assign will cause wild pointer problems if you modify the object. It is safe to modify primitive data types. The pointer is not automatically null after the decorated object is released, and sending messages to the object crashes.

The difference between __block and __weak modifiers:

__block can be used in both ARC and MRC mode, and can modify objects as well as primitive data types.

__weak can only be used in ARC mode and can only modify objects (NSString), not primitive data types (int). __block objects can be reassigned within blocks, __weak cannot.

Differences between Copy and retain:

Copy creates a new object, which means that the two objects have the same content. The new object retains 1, regardless of the reference count of the old object. Copy reduces the dependency of objects on context.

Retain creates a pointer that refers to the object count plus 1, indicating that the two objects have the same address (create a pointer, copy the pointer), and the contents of the object are the same. The retain value of the object is +1. That is, retain is a pointer copy and copy is a content copy.

The assign and retain:

Assign: simple assignment without changing the index count. Assign: NSString *newPt = [pt assing];

At this point, newPt is exactly the same as pt. The address is 0Xaaaa and the content is 0X1111, so newPt is just an alias of pt.

Assign is a direct assignment;

Retain uses a reference count. Retain causes the reference count to be incremented by 1 and release causes the reference count to be decrement by 1. When the reference count is 0, dealloc is called and memory is reclaimed. Retain: NSString *newPt = [pt retain];

At this point, the address of newPt is no longer 0Xaaaa, it may be 0Xaabb but the content is still 0X1111. So both newPt and pt can manage the memory where “ABC” is, so retainCount needs to be increased by 1;