Are instance variables set to nil by default in Objective-C?

后端 未结 3 1425
情书的邮戳
情书的邮戳 2020-12-15 15:53

I\'m sorting out some memory issues with my iPhone app and I\'ve just been thinking about some basics. If I setup an ivar and never end up using it in the lifespan of my obj

相关标签:
3条回答
  • 2020-12-15 16:10

    I find that it is good practice to always set those ivars to nil in the init method. That way, you are absolutely sure that your call to release in the destructor can not cause problems.

    If it turns out that Objective-C does automatically set them to nil, and for some reason you find yourself with a speed bottleneck that can be improved upon by removing those assignments (highly unlikely), then you can worry about removing them. In the meantime, set them all to nil and sleep easier :)

    update: BJ Homer and Chuck have pointed out that the ivars will automatically be set to zero, so now it comes down to a decision on style.

    0 讨论(0)
  • 2020-12-15 16:15

    Instance variables are initialized to 0 before your initializer runs..

    0 讨论(0)
  • 2020-12-15 16:28

    Yes, ivars are always initialized to 0/nil/NULL/NO/etc.

    However, if it helps you understand what's going on, go for it. The performance impact is negligible. You don't need to do it, but it won't cause any problems if you do.

    0 讨论(0)
提交回复
热议问题