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

KardelSharpeye

macrumors member
Original poster
Apr 28, 2009
56
0
Good morning,

I was just wondering if i have an array in header file declared as:
NSArray *myArray;
NSMutableArray *myMuArray;

and in my implementation file i have:
myArray = [NSArray arrayWithObjects:eek:bj1, obj2, nil];

myMuArray = [[NSMutableArray alloc] init];
[myMuArray addObject:eek:bj1];
[myMuArray addObject:eek:bj2];

Question 1:
what is the proper way to dispose these allocated memory?
[myArray release] for the array and...
[myMuArray removeAllObjects] ???

Question 2:
It seems like i can only remove and add items in array but is there anyway i can reassign the value of an item in an array?
something like myArray = 20 or myMuArray = 30??

Thanks in advance,
 
Question 1:
what is the proper way to dispose these allocated memory?
[myArray release] for the array and...
[myMuArray removeAllObjects] ???
Releasing an array automatically removes and releases all of its objects, so release it when you're done with the entire array, and removeAllObjects when you want to keep the array but empty it.

There is no garbage collector on the iPhone. myArray is autoreleased and doesn't need a call to release, but myMuArray does.

Question 2:
It seems like i can only remove and add items in array but is there anyway i can reassign the value of an item in an array?
something like myArray = 20 or myMuArray = 30??


Use the replaceObjectAtIndex:withObject: method.
 
Good morning,

I was just wondering if i have an array in header file declared as:
Code:
NSArray *myArray;
NSMutableArray *myMuArray;

and in my implementation file i have:
myArray = [NSArray arrayWithObjects:obj1, obj2, nil];

myMuArray = [[NSMutableArray alloc] init];
[myMuArray addObject:obj1];
[myMuArray addObject:obj2];
Question 1:
what is the proper way to dispose these allocated memory?
[myArray release] for the array and...
[myMuArray removeAllObjects] ???

Question 2:
It seems like i can only remove and add items in array but is there anyway i can reassign the value of an item in an array?
something like myArray = 20 or myMuArray = 30??

Thanks in advance,

First, I suggest putting your code in [ CODE ] tags to make it easier for us to read (and avoid the smilies). Use the # icon in the toolbar.

Question 1: Since you alloc'd your myMuArray, just do a [myMuArray release].

Question 2: myArray is not mutable, so you can't even remove or add items. But for NSMutableArray, in the class reference you'll find a number of replace... methods.
 
First, I suggest putting your code in [ CODE ] tags to make it easier for us to read (and avoid the smilies). Use the # icon in the toolbar.

Question 1: Since you alloc'd your myMuArray, just do a [myMuArray release].

Question 2: myArray is not mutable, so you can't even remove or add items. But for NSMutableArray, in the class reference you'll find a number of replace... methods.

cool thanks guys!:) eveything works! no more :(
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.