Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
in my awakeFromNib method i have added most of my NIB objects into mutable arrays so i can call them in my code. is it ok to release those objects using fast enumeration in the dealloc method?

Code:
- (void)dealloc
{
for (id objects in array1)
	[objects release];

[array1 release];
		
for (id objects in array2)
	[objects release];

[array2 release];
[super dealloc];
}
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Why would you need to? Why not let the array just hold onto the last reference, so that by releasing the array, you release all the objects?
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i'm setting properties for all of the objects, including the array objects. so even if i add those objects to an array in the init method, don't i still have to release each object, and not just the array that holds them?

but anyway, i've realized that since they are mutable arrays, the contents of the arrays with be changing through out the program, so i'll just have to release each one individually in dealloc to make sure they all get released.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
I see. Honestly, I'm curious why you're putting them in an array in the first place. It seems redundant. Why not just have them as ivars if you've already got @properties for them?

Furthermore, it is indeed OK to -release objects in an array in fast enumeration. You can even modify their contents. The only restriction is that you don't modify the array structure itself—no addObject:, removeObject: calls that modify the pointers that the array contains or the size of the array.

(NOTE: The same does not hold true of sets or keys in dictionaries. Once those objects get put into the collection, you should not modify them at all, because you might change the hash value, causing the collection to lose track of the objects.)
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
thanks.

the reason i'm throwing objects into arrays is because i'm sometimes animating most of them with the same movements, except for one or two, which i remove from the array before animation. it's code refactoring. instead of listing 20 variables within several animation methods, removing an object or two before calling animations on one NSMutableArray using fast enumeration is easier to maintain.
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
thanks.

the reason i'm throwing objects into arrays is because i'm sometimes animating most of them with the same movements, except for one or two, which i remove from the array before animation. it's code refactoring. instead of listing 20 variables within several animation methods, removing an object or two before calling animations on one NSMutableArray using fast enumeration is easier to maintain.

Shouldn't you be using inheritance, so each class knows how to perform its movements? Removing items from an array before performing an operation on the entire array is inefficient and kinda defeats the purpose of using an object-oriented language...

Also, collection classes retain objects added to them and release them on dealloc. If the objects you put into an array are autoreleased after creation (or created with factory methods) then you don't have to worry about releasing the individual members of the array.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
Shouldn't you be using inheritance, so each class knows how to perform its movements? Removing items from an array before performing an operation on the entire array is inefficient and kinda defeats the purpose of using an object-oriented language...

not sure i follow you. i'm adding objects like UIButtons, UIImageViews and Labels to the array. if a certain button is clicked (or tapped since it's on iPhone), that button is removed from the array and enlarged, while all the other objects within the array are animated off screen. but if a different button was tapped, the first button would remain in the array so it too would be animated off screen. which ever button is tapped, it will execute a different animation than all the other objects in the array. all these buttons, views and labels are members of an elite squad known as the special victims unit *record scratch* sorry... they're all members of a view controller class, so i'm not sure what you mean by suggesting the use of inheritance in animating select objects.
 

autorelease

macrumors regular
Oct 13, 2008
144
0
Achewood, CA
not sure i follow you. i'm adding objects like UIButtons, UIImageViews and Labels to the array. if a certain button is clicked (or tapped since it's on iPhone), that button is removed from the array and enlarged, while all the other objects within the array are animated off screen. but if a different button was tapped, the first button would remain in the array so it too would be animated off screen. which ever button is tapped, it will execute a different animation than all the other objects in the array. all these buttons, views and labels are members of an elite squad known as the special victims unit *record scratch* sorry... they're all members of a view controller class, so i'm not sure what you mean by suggesting the use of inheritance in animating select objects.

Okay, I see what you mean. I couldn't really tell what you were trying to accomplish from your previous post.

You should probably keep one array for all objects, and create another one just for the objects you want to animate. Put the objects you want to animate into this second array before you start the animation, and empty it when the animation is finished.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Okay, I see what you mean. I couldn't really tell what you were trying to accomplish from your previous post.

You should probably keep one array for all objects, and create another one just for the objects you want to animate. Put the objects you want to animate into this second array before you start the animation, and empty it when the animation is finished.

Or keep one array, period:
Code:
theOne = clickedThing;
for(id myThing in myControls) {
  if(myThing == theOne) continue;
  [myThing animate];
}

-Lee
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Since you are simply sending a message to every object, why not just us NSArray's instance method:
Code:
- (void)makeObjectsPerformSelector:(SEL)aSelector

Meaning something like..
Code:
 [myArray makeObjectsPerformSelector: @selector(release) ];

I would think this is as fast or faster.
 

Littleodie914

macrumors 68000
Jun 9, 2004
1,813
8
Rochester, NY
Maybe I misunderstood your question, but why not just release the objects once they are placed into the array in the beginning? Eg:

Code:
- (void)awakeFromNib {
    NSMutableArray arrayOfThings = [[NSMutableArray alloc] init];
    [arrayOfThings addObject:tableView];
    [tableView release];
    [arrayOfThings addObject:textField];
    [textField release];
}

When you call 'release' on an array, it also sends release to all of its contained elements. So if the retain count of any of the objects in your array is '1', they will be automatically released with the array. Eg:

Code:
- (void)dealloc {
    [arrayOfThings release]; // Release is also sent to all elements
    [super release];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.