Objective c, Memory management of instance members

后端 未结 6 797
南方客
南方客 2020-12-02 00:58

I am confuzed by the memory management of instance members. I have a class with a ivar:

DetailedResultsTableViewController *detailedResultsTableViewControlle         


        
6条回答
  •  情话喂你
    2020-12-02 01:28

    Your @interface is correct, but your implementation slightly wrong:

    @implmentation MyClass
    
    //It's good practice to explicitly state the a variable name for this property to use 
    //The common approach is to use the property name with a leading underscore
    //This prevents accidentally accessing the ivar within the class instead of using the     accessor methods. You should only access the ivar directly within the accessor methods (which in these case are being created for you by  @synthesize), in the designate init method and dealloc
    @synthesize detailedResultsTableViewController = _detailedResultsTableViewController;
    
    -(void)dealloc
    {
    //...
        [_detailedResultsTableViewController release];
    //...
    }
    
    @end
    

    When accessing the property:

    myClass.detailedResultsMapViewController = [[[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil] autorelease];   
    

    The code that is setting the method does not 'own' the new value therefore it must autorelease.

提交回复
热议问题