Acceptable ways to release a property

后端 未结 4 527
滥情空心
滥情空心 2020-12-19 12:31

Assume there is a class with the following interface:

#import 

@interface MyClass :          


        
4条回答
  •  心在旅途
    2020-12-19 12:50

    First, if you want to avoid the alloc, release, autorelease etc... you can call a date factory method which doesn't start with alloc.

    For example: self.myDate = [NSDate date];

    The date class factory method does an autorelease according to the convention rules. Then the property retains it.

    1. Alloc will give it a retain count of 1, then assigning the property will retain it. Since your class is now retaining it from the property, you can release it to counter act the alloc.

    2. Ditto but that's a wierd round about way to do it.

    3. 3 is equivalent to the code I had above ([NSDate date]);

    4. In this case, the property will retain it (after alloc incremented the retain count), then you're going under the covers to decrement it. Works but I wouldn't recommend doing that since you're synthesized (retain) property will do that for you.

提交回复
热议问题