alloc + init with synthesized property - does it cause retain count to increase by two?

前端 未结 4 976
生来不讨喜
生来不讨喜 2021-02-05 10:23

I\'ve seeen the following snippet quite a bit:

In the header:

SomeClass *bla;
@property(nonatomic,retain) SomeClass *bla;

In the implem

4条回答
  •  长发绾君心
    2021-02-05 10:39

    Yes, you are right - using the synthesized setter of a retain property would increase the ref-count on an instance you already own (as alloc implies ownership).

    Just go with the second form you mentioned in your initializers:

    _bla = [[SomeClass alloc] init];
    

    ... and remember to fix the retain count otherwise, e.g.:

    self.bla = [[[SomeClass alloc] init] autorelease];
    

提交回复
热议问题