I declare my array in a * .h file:
@interface aViewController: UIViewController
{
NSMutableArray * anArray; // You will need to later change this many times.
}
@end
I allocate my * .m file memory for it:
- (void) viewDidLoad
{
anArray = [[NSMutableArray alloc] init];
}
I press the test button to load my array (in the end, it will need to load the DIFFERNT values each time it is clicked):
anArray = [NSMutableArray arrayWithObjects: @ "one", @ "two", @ "three", nil];
And I release him here:
- (void) dealloc
{
[anArray release];
[super dealloc];
}
Does everything look normal?
Since it crashes when I run this code later:
NSLog (@ "% d", [anArray count]);
Not sure why the simple "NSLog () and counter" crash everything.
Dirk
Let me say this: I have a HUGE misunderstanding of pointers, arrays, strings, and memory.
I read everything I can find on it ... but (for now) to find a simple, understandable, and understandable description.
Can you offer it? (We hope there are less than 10 pages of reading.) Is there a link that explains JUST to this topic ... and in terms of “you have 12 years of coding experience ... but NONE that has ever dealt with memory or pointers” .)
So, is the variable name NOT the way I refer to the object? Then why?
I am used to many other languages that just do this:
myString = "this"
myString = "that"
myInt = 5
myInt = 15
(Which could be simpler.)
Seems like this would be the easiest way to do this. (And it works, but is it really right?)
Don't allocate any memory for my NSMutableArray initially.
Don't re-alloc any memory repeatedly. (When I change my array values.)
Don't release it repeatedly. (Before I change my array values.)
Don't release it when I exit the program.
But:
Always remember to use RETAIN when I initially assign
(and repeatedly reassign) new values to my anArray variable.
You do not load your array anArray = [NSMutableArray arrayWithObjects: @ "one", @ "two", @ "three", nil]; Instead, you replace it with a new instance and worse: with an instance whose link actually belongs to some organization that you do not manage.
Wow. So, I could have 20 arrays ... everyone called the same name: anArray ... and everything would be different? (There is no such thing as a GLOBAL array?)
etc .. To clear the old values, the removeAllObject method may be useful. There are also mutation methods that can be used to add multiple values at once.
So ... first I need to “delete all objects” ... and then I can call the ONE method to add all my new values again.
anArray = [[NSMutableArray arrayWithObjects: @ "one", @ "two", @ "three", zero] save]; instead of the alloc / init sequence.
Wow. I thought that nothing could be stored in the array without allocating space for it.
If you are really going to replace the entire array, you can consider using the properties
How do I do this using properties? What would be the correct way to do something like this:
> anArray = [NSMutableArray arrayWithObjects: @ "one", @ "two", @ "three", nil];
> anArray = [NSMutableArray arrayWithObjects: @ "four", @ "five", @ "six", nil];
As I:
x = 12;
x is 24;
Wow. I REALLY misunderstand everything about strings, arrays, and memory. I thought the “easy way” is to use ONCE ... use a mutable array ... change it as much as you want ... and free it up ONCE.
The problem with this is that this new array is not saved,
I would think that the old array will disappear ... and you can use the new array. (But I think not.)
In addition, you have a memory leak because you never freed the original array.
I thought the old array should not have been freed ... I did not do this ... I just want to CHANGE it so that it contains my new values. (But I think not.)
but you need to use [anArray release];
I thought it would make me free the memory I allocated ... (but I think not) ... and then I would have to reallocate more memory. (But I think not.)
anArray = [[NSMutableArray arrayWithObjects: @ "one", @ "two", @ "three", zero] save];
So I have to "save" this ... so that it does not disappear from under me? (I don’t know why this will happen. Until I talk about it ... in my final call to dealloc.)
Another probably more correct way to fix this would be to use addObject: or addObjectsFromArray: NSMutableArray methods instead of constantly creating new arrays.
I want to create only one array ... and just use it the way I want. I never want to add ADD to an array. I want to set it to my new values.