Do I need to remove NSMutablearray objects before NULL/deleting NSMutablearray - ios

NSMutableArray *thisArray = [[NSMutableArray alloc] initWithObjects:#"one", #"two", #"three", nil];
Do I need to delete any objects before deleting thisArray?
thisArray = NULL;

A NSArray/NSMutableArray only keeps a reference to the objects it contains, and by setting that array to NULL it will automatically releases all those references. You don't have to cycle through them all to release them manually.

Related

How to initialize NSMutableArray in iOS

In my app there is huge number of array lists. That's why I have added all arrays in one main array list, and I have initialized them using a "for" loop.
I am getting error inside the "for" loop: "Fast enumeration variables can't be modified in ARC by default".
NSMutableArray * MainArray = [[NSMutableArray alloc] initWithObjects:NameArray, IdArray, MasterIdNameArray, MasterIdArray, nil];
for (NSMutableArray * array in MainArray) {
array = [[NSMutableArray alloc] init];
}
Yes, you can not modify the values of array in a fast enumeration, i.e. for(x in Array). The object x becomes constant, hence it would through a warning.
However you can use for(int i=0; i<[MainArray count]; i++) loop to achieve this.
But, wait: Why you want to initialize it after adding it to an array. Do it like this:
//first create all the arrays that you have,
//NameArray
//IdArray
//MasterIdNameArray
//MasterIdNameArray
//then add them in the MainArray
NSMutableArray *nameArray = [NSMutableArray array];
NSMutableArray *idArray = [NSMutableArray array];
NSMutableArray *masterIdNameArray = [NSMutableArray array];
NSMutableArray *masterIdNameArray = [NSMutableArray array];
NSMutableArray *mainArray = [#[nameArray, idArray, masterIdNameArray, masterIdNameArray] mutableCopy];
Note: I renamed all the variable for the shake for Naming Conventions in Objective-C.
SeanChense is correct. You cannot put an array without initializing it.
NSMutableArray * MainArray = [[NSMutableArray alloc]init];
for (int i = 0;i < YOURCOUNTHERE;i++) {
NSMutableArray * array= [[NSMutableArray alloc]init];
[mainArray addObject:array];
}
If your NameArray is nil, MainArray is nil.
You can do it likes:
NSMutableArray *mainArray = [#[] mutableCopy];
for (int i = 0;i < 3;i++) {
[mainArray addObject:[#[] mutableCopy]];
}
In my app there is huge number of array lists. That's why I have added all arrays in one main array list, and I have initialized them using a "for" loop.
You appear to be misunderstanding how variables and reference types work. Maybe the following will help:
Your line of code:
NSMutableArray *mainArray = [[NSMutableArray alloc] initWithObjects:nameArray, idArray, masterIdNameArray, masterIdArray, nil];
copies the references stored in each of the variables nameArray, idArray etc. and stores those references in a new array.
Somewhere you must declared each of these variables, e.g. something like:
NSMutableArray *nameArray;
This declares a variable nameArray which can hold a reference to a mutable array, that is a reference of type NSMutableArray *. The variable is initialised with the value nil - the "no reference" value.
When your first line of code is executed the value in each variable is passed in the method call, not the variable itself, so the call is effectively:
NSMutableArray *mainArray = [[NSMutableArray alloc] initWithObjects: nil, nil, nil, nil, nil];
and mainArray is set to reference a new mutable array with zero elements - as all references before the first nil in the argument list are used as the initial values in the array.
After mainArray has been setup in this way any operation on it has no effect on the values stored in variables nameArray et al. - there is no connection to those variables. In your loop:
for (NSMutableArray *array in mainArray) {
array = [[NSMutableArray alloc] init];
}
The variable array is a new variable, which is set in turn to each of the values in mainArray. The variable array does not become an alias for each of the variables nameArray et al. in turn - mainArray holds values not variables.
HTH and you now understand why your code could never do what you intended - that is set the values stored in the variables nameArray et al..

Add other object to NSArray

I made a small code
NSArray* _options = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageNamed:#"2"],#"img",name,#"text"
, nil],nil];
Now, I want add other object to _options. What should i do?
I make more test but no success.
Thank for all
you can use [NSArray arrayByAddingObject:]
_options = [_options arrayByAddingObject:object];
or change _options to NSMutableArray
NSMutableArray *_options = [NSMutableArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:[UIImage imageNamed:#"2"],#"img",name,#"text"
, nil],nil];
[_options addObject:object];
and you may want to use modern syntax
NSMutableArray *_options = [#[#{#"img":[UIImage imageNamed:#"2"],#"text":name}] mutableCopy];
[_options addObject:object];
NSArray does not allow any changes to be made; you can use an NSMutableArray instead like this:
NSMutableArray *mutable = [_options mutableCopy];
[mutable addObject:yourObject];
NSDictionary is same in that it can't be mutated.
You can't add objects to a NSArray, to do so, you need a NSMutableArray.
However, you can add objects to NSArray when creating it with : arrayWithObjects
First, create an NSMutableArray, as you can make changes to it as you see fit later throughout your code:
NSMutableArray *newOptions = [NSMutableArray alloc]init];
[newOptions setArray:_options];
[newOptions addObject:yourObject];

NSMutableArray with init and mutableCopy, how to autorelease?

I've gone crazy trying to find a leak with an NSMutableArray:
NSMutableArray *mutablearray =[[[[NSMutableArray alloc] initWithArray: array] mutableCopy] autorelease];
Finally I understood that I need to autorelease twice my mutablearray because initWithArray is +1 and mutableCopy is +1 too.
Then I'm doing:
NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array] autorelease];
mutablearray = [[mutablearray mutableCopy] autorelease];
But, it's correct to do?:
NSMutableArray *mutablearray = [[[[[NSMutableArray alloc] initWithArray: array] autorelease] mutableCopy] autorelease];
Thanks
The first question comes in mind is WHY you are initializing and also making a mutable copy at the same time initWithArray gives a new instance with new memory and you can use it.Then why creating a mutable copy of it?
Use
NSMutableArray *mutablearray = [[[NSMutableArray alloc] initWithArray: array]autorelease];
This gives you a mutable instance .So no need for calling mutablecopy anyway
OR
NSMutableArray *mutablearray = [[array mutableCopy]autorelease];

iOS: Difference between NSMutableArray alloc/init and arrayWithObjects:

What is the difference between
NSMutableArray* p = [[NSMutableArray alloc] initWithObjects:...]
and
NSMutableArray* p = [NSMutableArray arrayWithObjects:...]
In the first one, you have the ownership of array object & you have to release them.
NSMutableArray* p = [[NSMutableArray alloc] initWithObjects:...];
[p release];
& last one you dont need to release as you don't have the ownership of array object.
NSMutableArray* p = [NSMutableArray arrayWithObjects:...]; //this is autoreleased
If you call release in this, then it will crash your application.
[NSMutableArray arrayWithObjects:] is the same as [[[NSMutableArray alloc] initWithObjects:] autorelease]
In practice, there is no difference if you're on ARC.
The latter basically is just a shorthand for [[NSMutableArray alloc] initWithObjects: ...], except the returned array is autoreleased (which is important if you're still doing manual reference counting).
What I think the difference is that: initWithObjects is a instance method, and arrayWithObject is a class method.

How can a NSMutableArray object be cleared from all elements

This is very basic but I don't know how to do it.
How can a NSMutableArray object be cleared from all elements. I want to set the array to the state when it was created:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
Thanks
[myArray removeAllObjects];
Docs can be found here

Resources