Objective C - Memory management question?

后端 未结 4 1229
-上瘾入骨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:07

    Yes, you can set an instance variable that way and it's totally fine — in fact, that's the preferred way to do it in init methods (because setters might have side effects). Just remember to release the old object (if any) in the variable before setting a new one or you'll leak. And whether you use properties or set the ivar directly, you need to release the object in dealloc.

提交回复
热议问题