Out of convenience often when I have a method where I need
to reuse the same local variable for different purposes I tend do something like this:
NSMutableArray *mutArray = [NSMutableArray arrayWithObjects: @"one", @"two", nil];
[self fooWithArray:mutArray];
mutArray = [NSMutableArray arrayWithObjects: @"three", @"four", nil];
[self barWithArray:mutArray];
//and
MyClass *myClass = [[MyClass alloc]initWithString:@"one"];
self.oneClass = myClass;
[myClass release];
myClass = [[MyClass alloc]initWithString:@"two"];
self.twoClass = myClass;
[myClass release];
Memory-wise, is this the right thing to do ?
Is this code prone to memory leaks ?
You don’t have to assign objects to variables before using them. The only reason I can see for doing that would be verbosity, but it’s not required.
You could get away with: