开发者

Why not use self. call inside class

开发者 https://www.devze.com 2023-04-12 22:13 出处:网络
I\'ve read google\'s and apple\'s code guide, they both access instance variables without self. call(getter and setter) inside the method implementation开发者_运维百科 even though they have declared a

I've read google's and apple's code guide, they both access instance variables without self. call(getter and setter) inside the method implementation开发者_运维百科 even though they have declared a property for that instance variable.That's why?

In my opinion, using self. call to set and get instance variables inside the method implementation of class makes it easier to manager retain count.

Is there any caveat to use getter and setter inside class?


It depends. You should always use the accessor in normal use.

However for init and dealloc methods, you should instead use the direct ivars to release and set variables. That's because the setters can have side effects that are not good to trigger during class initialization or deallocation.

In practice using an accessor as part of init probably will not cause an issue. But I have seen a number of real world crashes where a custom setter was not expecting nil and so use of the accessor in dealloc crashed the app. Even if it didn't crash it could be doing a lot of pointless work since the class was about to die.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号