Understanding when to call retain on an object?

前端 未结 4 458
礼貌的吻别
礼貌的吻别 2021-01-12 21:01

When should retain be used ? I understand that it increments the object references count, so basicly the next release on that object will not call

4条回答
  •  余生分开走
    2021-01-12 21:48

    Marcel, memory management is a crucial point developing iOS Apps. You should consider read the Apple documentation about it as suggested by the other guys.

    I can add here some information to try to help you with your needs.

    The memory management process with Obj-C is count base. It means any time you 'retain' some object the system improves a counter for this object. For example, if you create a button and 'retain' it, it will have a value 1. If you retain it again, it will have a value 2. To destroy this object completely, you will need to release it twice. Any time an object has a value 0 it will be destroyed.

    My personal opinion: If you want to have a good control about the memory management of your app, it's good to explicitly retain and destroy your objects, avoiding autorelease when possible. Again, it's my personal opinion. I like to be aware of the memory process inside my Apps, that's why I prefer to take care of this precisely.

    Sure it's not the BEST way, each developer will prefer to use a different approach managing memory.

    Again: take some time to read the documentation Abizem suggested you. Sure you will have a better understanding about it.

提交回复
热议问题