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

后端 未结 3 1428
情书的邮戳
情书的邮戳 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.

提交回复
热议问题