Memory management technique for Objective-C iVars/properties

Posted by David Rea on Stack Overflow See other posts from Stack Overflow or by David Rea
Published on 2010-06-14T23:51:52Z Indexed on 2010/06/15 0:02 UTC
Read the original article Hit count: 251

Is the following code doing anything unnecessary?

@interface MyClass {
   NSArray   *myArray;
}

-(void)replaceArray:(NSArray *)newArray;

@implementation MyClass

-(void)replaceArray:(NSArray *)newArray {
   if( myArray )
   {
      [myArray release];
      myArray = nil;
   }

   myArray = [[NSArray alloc] initWithArray: newArray];
}

@end

What if I made the following changes:

1) Made myArray a property:

@property (nonatomic, retain) NSArray myArray;

2) Changed the assignment to:

self.myArray = [NSArray arrayWithArray: newArray];

Would that allow me to remove the conditional?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about memory