Object allocate and init in Objective C

后端 未结 6 2165
日久生厌
日久生厌 2020-12-07 08:24

What is the difference between the following 2 ways to allocate and init an object?

AController *tempAController = [[AController alloc] init];
self.aControll         


        
6条回答
  •  一个人的身影
    2020-12-07 09:08

    You could also do

    @property (nonatomic, retain)AController *aController;
    ...
    self.aController= [[AController alloc] init];
    [aController release];
    

    with a retaining property, and it would function the same way, but its better to use the other way (for retaining properties) because it's less confusing, that code makes it look like you assign aController and then it gets deleted from memory, when actually it doesn't because setAController retains it.

提交回复
热议问题