Release, retain, autorelease in Objective C, coupled with garbage collection, are difficult to fully understand for beginners who are often confused. Since release exists, why autorelease? Is autorelease garbage collection? Let's take a look at the clutch control in the automobile gearbox.
This can be simplified to consider:
I. No garbage collection
Retain/release is the most basic. One is to increase the count, and the other is to reduce the reference count. After an object is created, we only need to release it when appropriate.
Autorelease is generally used in static functions such as stringwithstring for object creation and initialization. It is similar to release. The only difference is that you do not need to manually release the data, there is a robot in the background to help you release.
2. Garbage Collection
Basically, you can forget release and autorelease, but use Retain as appropriate to prevent early recovery by the system.
This reminds me of AT, MT, and AMT in the automotive speed control system. The clutch processing method is as follows:
Mt is a manual file. You need to step on the clutch by yourself, which is equivalent to release.
At is an automatic file with no clutch, which is equivalent to system garbage collection.
AMT is an automatic shift function based on manual shift. It is equivalent to asking a driver to help you step on the clutch. It is equivalent to autorelease. On the surface, you don't have to step on the clutch and release on your own, in fact, some people help you.