Objective C - Memory management question?

后端 未结 4 1273
-上瘾入骨i
-上瘾入骨i 2021-01-28 21:35

I know that when i have a property for an object which is set to retain or copy i must do the following.

Object *o = [[Object alloc] init];
self.myObject = o;
[o         


        
4条回答
  •  长情又很酷
    2021-01-28 22:06

    Because Jacob already answered the part of without property. I will answer the left

    Can i do the following without having a leak?

    myObject = [[Object alloc] init];

    You have to release the myObject somewhere, maybe in the delegate method, or maybe after you finish using it, but remember to release the object, especially before you lose the object reference

    Is it better to define a property for every variable I have in every class? Why??

    It depends on your coding convention, define all properties will help you with memory management a lot, but it comes with burden and sometimes your team doesn't want to define instance variable, property and synthesize for every variable, which means quite a lot of codes. So, discussing with your team to see what is your weakness and strength. If you are weak at memory management, I recommend to define for every

提交回复
热议问题