proper memory handling for an NSMutableArray assigned to property?

前端 未结 3 1417
余生分开走
余生分开走 2021-01-28 04:52

I have a property declared like this:

@property (nonatomic, retain) NSMutableArray *pricingLevels;

And I assign it like this:

          


        
3条回答
  •  无人共我
    2021-01-28 05:46

    self.pricingLevels is a property declared as retained which means every time you set it thru property assignment (the dot-syntax OR the method), the object automatically retains the object for you.

    self.pricingLevels = [NSMutableArray array];
    [self setPricingLevels:[NSMutableArray array]];
    

    The above code will do the same and automatically retain the array passed. This is what happens under the hood (or something similar). This method gets called:

    - (void)setPricingLevels:(NSMutableArray *)a {
        if(_pricingLevels != a) {
            [_pricingLevels release];
            _pricingLevels = [a retain];
        }
    }
    

    You see? Automatically retained, while the previous value automatically gets released.

    EDIT to answer your last question: Yes you should call autorelease

提交回复
热议问题