SoFunction
Updated on 2025-04-12

iOS object properties detailed introduction

iOS object properties

Some properties of the oc object:

retain,strong, copy,weak,assign,readonly, readwrite, unsafe_unretained

Let’s talk about their respective functions and differences:

retain,The counter is added by 1, (adding a pointer to memory) The corresponding release (counter-1) setter method releases the parameters, and then retains the new value. All implementations are in this order.

- (void)setBackView:(UIView *)backView {

  if (_backView != backView) {

    [_backView release];

    _backView = [backView retain];
  }

  return _backView;
}

copy, copy, new address, content copy, setter methods perform Copy operations. Just like the retain processing process, first release the old value, then Copy to release a new object, retainCount is 1. This is a mechanism introduced to reduce dependence on the context.

(Take a little deep copy shallow copy, shallow copy deep copy)

This article is more intuitive (/omegayy/article/details/7311839)

Official documentation (/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/)

What exactly does the behavior after the copy and mutableCopy call are done depends on how the NSCoping and NSMutableCopy protocols are implemented.

 strong, Strong reference, the counter is increased by 1, the same as retain (corresponding to retain and copy)

weak,Weak quotes

strong Used to modify strong reference attributes;

@property (strong) SomeClass * aObject; 
Corresponding to the original 
@property (retain) SomeClass * aObject; and @property (copy) SomeClass * aObject; 

weak Used to modify weak reference attributes;
@property (weak) SomeClass * aObject; 
Corresponding to the original 
@property (assign) SomeClass * aObject; 

__weak, __strong is used to modify variables, and in addition __unsafe_unretained, __autoreleasing is used to modify variables.
__strong is the default keyword.
__weak declares a weak reference that can be automatically nilized.
__unsafe_unretained declares a weak application, but will not automatically nilize, that is, if the memory area pointed to is freed, this pointer is a wild pointer.
__autoreleasing is used to modify the parameters of a function, which will be automatically released when the function returns.

The difference between strong and weak

The difference is that when an object no longer has a pointer of type strong pointer to it, it will be released, even if there is a pointer to it with weak type.

Once the last strong pointer leaves, the object will be released and all remaining weak pointers will be cleared.

There may be an example to describe it as appropriate.

Imagine that our object is a dog, and the dog wants to run away (being released).

A strong pointer is like a dog tied up. As long as you hang the dog with a leash, the dog will not run away. If 5 people are holding a dog (5 strong pointers pointing to 1 object), the dog will not run away unless all 5 leashes fall off.

The weak pointer is like a child pointing at the dog and shouting, "Look! A dog is there." As long as the dog is tied, the child can see the dog, and the (weak pointer) will always point at it. As long as the dog's leash falls off, the dog will run away, no matter how many children are watching it.

As long as the last strong pointer no longer points to the object, the object will be released and all weak pointers will be cleared.

assign,For simple types, the counter is not added, the value is directly assigned, a pointer, a block address, and the setter method is directly assigned without any retain operations. In order to solve the problem of original type and loop reference

readonly, read-only, only generates get method, no set method

readwrite,Default, readable and writeable, set and get methods will be generated

unsafe_unretained, similar to weak, the so-called unSafe refers to the situation where pointers are prone to occur, use it with caution

setter = xxxx, declare the set method of the object

getter = xxxx, the get method that declares the object

nonatomic, Non-atomic operations, the system does not add code, and the running speed is relatively fast, but data operations are relatively unsafe in multi-threaded situations

atomic,Atomic operations, a lot of lock-up unlocking code will be added during compilation, and data operations are relatively safe in multi-threaded situations.

* Use assign: for basic data types (NSInteger) and C data types (int, float, double, char, etc.)
* Using copy: for NSString
* Use retain: for other NSObjects and their subclasses

Thank you for reading, I hope it can help you. Thank you for your support for this site!