1. What are the attribute keywords?

classification Attribute keyword
atomic Atomic, nonatomic
Read and write access Readwrite, ReadOnly, setter, getter
Memory management Assign, weak, unsafe_unretained, retain, strong, and copy
Nullable sex (_Nullable, __nullable),

Nonnull, _Nonnull, __nonnull),

(NULl_unspecified, _Null_unspecified, and __NULl_unspecified),

null_resettable

1.1 atomic

Attribute keyword usage
atomic Atomicity (default). The compiler automatically generates mutex locks for setter and getter methods, ensuring thread-safe assignment of properties and atomic operations on values, but excluding operations and access.

For example, if an atomic modifier is an array, assigning and evaluating the array is thread-safe. However, if we operate on an array, such as adding or removing objects from the array, it is not atomic’s responsibility, so adding or removing objects from an atomic array is not thread-safe.
nonatomic Nonatomic, general properties are modified with nonatomic because atomic is very time consuming.

1.2 Read and Write Permissions

Attribute keyword usage
readwrite Readable and writable (default), generating declarations and implementations of both setter and getter methods.
readonly Read-only, only generates the declaration and implementation of getter methods.
setter You can specify the generated setter method name, such as setter = setName.
getter You can specify the generated getter method name, such as getter = getName.

1.3 Memory Management

Attribute keyword usage
assign 1. You can modify both basic data types and object types;

2. The implementation of setter methods is direct assignment, generally used for basic data types;

Modify basic data types, such as NSInteger, BOOL, int, float, etc.

4. Modify the object type without increasing its reference count.

Overhang pointer: After the assigned object is released, the pointer will still point to the original address of the assign object. The overhang pointer will become the overhang pointer. Continuing to access the original object through this pointer may cause the program to crash.
weak 1. Only object types can be modified.

2. It can only be used in ARC mode.

3. Modify weak references, do not increase the object reference count, mainly used to avoid circular references;

Weak The pointer is set to nil automatically after being freed. Weak does not produce dangling Pointers.
unsafe_unretained 1. You can modify both basic data types and object types;

2. It is often used under MRC, but rarely used under ARC;

3. The same as weak, the difference is that unsafe_unretained generates dangling Pointers.
retain 1. In MRC, strong is used in ARC;

2. Modify the strong reference, the original pointer to the old object, then point to the new object, and increase the reference count of the new object by 1;

3. The implementation of the setter method is to release the old value and retain the new value for the OC object type.
strong 1. It can only be used under ARC.

2. Same principle as retain;

3. But when modifying blocks, strong equals copy and retain equals assign.
copy The implementation of setter methods is release old values, copy new values, for NSString, block, etc.

1.4 can be empty

See iOS Mashup | Specifying nullability for Objective-C apis.

2. Ownership modifier

Ownership modifier usage
__strong 1. Strong reference objects can correspond to the keywords strong, retain, and copy.

2. The compiler generates instance variables with __strong ownership modifier for strong, retain, and copy attributes.
__weak 1. Weak references hold objects, corresponding to the weak keyword, which is used in ARC to prevent circular references.

2. The compiler generates an instance variable with an __weak ownership modifier for the weak attribute.
__unsafe_unretained 1. Weak references hold objects, corresponding to the keywords unsafe_unretained and assign, which are used to prevent repeated references under MRC.

2. The compiler generates an instance variable with the __unsafe_unretained ownership modifier for the unsafe_unretained property.

3. Compared to __weak, it does not need to traverse the weak table to check if the object is nil, which is better performance. But it produces dangling Pointers.
__autoreleasing In MRC we can register an object with autoreleasepool by sending an Autorelease message to it, and in ARC we can use the __autoreleasing modifier to modify the object to register it with autoreleasepool.

For a detailed explanation of ownership modifiers, see iOS – The Cliche of Memory Management (iii) : ARC Is Available.

3. Relevant interview questions

Q: How are atomic modified properties thread-safe?

A: The compiler automatically generates mutex locks for setter and getter methods to ensure thread-safe assignment and atomicity operations on properties, but not operations and access. For example, if an atomic modifier is an array, assigning and evaluating the array is thread-safe. However, if we operate on an array, such as adding or removing objects from the array, it is not atomic’s responsibility, so adding or removing objects from an atomic array is not thread-safe.

Q: When is the weak/__weak keyword used?

  • In order to avoid circular references in ARC, one of the objects that reference each other can be usedweak/__weakWeak reference modifier, often used in pairsdelegateandblockThe modification;
  • ② Interface Builder IBOutlet controls are generally usedweak.

Q: What are the differences between assign and weak?

  • 1.weakCan only modify objects, andassignYou can modify both objects and primitive data types;
  • 2.assignAfter the modified object is released, the pointer still points to the original object address; whileweakModified objects automatically set pointer to nil when freed;
  • ③ Similarities: When modifying objects,assignandweakDoes not change the reference count of the object.

Q: What’s wrong with the following code? (Light and dark copy)

@property (copy) NSMutableArray *array;
Copy the code

A: No matter whether the assigned value is an NSMutableArray or an NSArray object, the copy operation is an NSArray object (deep copy). Because the property is declared as NSMutableArray type, it is inevitable that there will be a caller to call its add object, remove object and other methods, at this time because the result of copy is NSArray immutable object, the NSArray object call add object, remove object and other methods, will produce program exceptions.