Acceptable ways to release a property

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

Assume there is a class with the following interface:

#import 

@interface MyClass :          


        
4条回答
  •  余生分开走
    2020-12-19 13:05

    5) is a bug - it leaks the old instance as it doesn't get released but just reassigned.

    1) is clean and the best way to go. 4) is ok but puts some burden on the memory system - the object might live longer than needed. 2) technically ok, but you shouldn't directly retain/release the property - that's what the syntactic sugar is for! 3) technically ok, but also bypasses the property and relies on implementation details.

    2) and 3) are discouraged and ask for trouble in the future when some part of code changes.

    Edit: New code doesn't leak in 5). It has the same downsides, though.

    There's a reason why we got support for properties, and it does a great and consistent use. You should only consider bypassing them if your time profile gives very clear hints that this is a bottle neck (unlikely).

提交回复
热议问题