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..
Related
Hi i am using the following code to remove objects from NSMutableDictionary, both dictionaries contains same array values, if i remove a value from D1 the same value get removed from D2 Automatically.
Help me out how to solve this,
NSMutableDictionary *D1=[[NSMutableDictionary alloc]init];
NSMutableDictionary *D2=[[NSMutableDictionary alloc]init];
NSMutableArray *arr_objs = [[NSMutableArray alloc]initWithObjects:#"ss",#"nn", nil];
[D1 setObject:arr_objs forKey:#"Keys"];
[D2 setObject:arr_objs forKey:#"Keys"];
[[D1 objectForKey:#"Keys"]removeObject:#"nn"];
arr_objs is the same array in two dictionaries. This
NSMutableArray *arr_objs = [[NSMutableArray alloc]initWithObjects:#"ss",#"nn", nil];
NSMutableArray *arr_objsCopy = [arr_objs mutableCopy];
[D1 setObject:arr_objs forKey:#"Keys"];
[D2 setObject:arr_objsCopy forKey:#"Keys"];
should give you what you're looking for. Rather than storing the same array in two dictionaries, this example creates two identical arrays that can later be modified without affecting each other.
I'm trying to rewrite one element from self.tableData to another.
my NSMutableArray:
self.tableData = [[NSMutableArray alloc] initWithObjects:
[[Cell alloc] initWithName:#"dawdw" andImage:#"dwddw" andDescription:#"dawdw" andTypes:#"dawwd dawwd" andforWho:#"dwaadw"],
[[Cell alloc] initWithName:#"Kabanos" andImage:#"spodwwdwdrt.jpg" andDescription:#"dwdw" andTypes:#"dwdw dww" andforWho:#"dawwd"],
[[Cell alloc] initWithName:#"dwwd" andImage:#"dwwd" andDescription:#"dwwd" andTypes:#"wdwd daww" andforWho:#"dadawwa"],nil];
NSMutableArray *newarray;
[newarray addObject:self.tableData[0]];
But it's not working, maybe it's a newbie question but i have never before worked with arrays with many objects inside.
With self.tableData[0] i men rewrite object
[[Cell alloc] initWithName:#"dawdw" andImage:#"dwddw" andDescription:#"dawdw" andTypes:#"dawwd dawwd" andforWho:#"dwaadw"],
NSMutableArray *newarray;
[newarray addObject:self.tableData[0]];
The problem with the code above is that you haven't created an array for newArray to point to, so the value of newArray is nil. Do this instead:
NSMutableArray *newarray = [NSMutableArray array];
[newarray addObject:self.tableData[0]];
Now newArray will point to a valid mutable array to which you can add objects.
Also, realize that even with the fixed code, newArray[0] will point to the very same object that you've stored in self.tableData[0], not a copy. If you want it to point to a different object that contains similar data, you should either make a copy of the object or instantiate a new one, e.g.:
[newarray addObject:[self.tableData[0] copy]];
or:
[newarray addObject:[[Cell alloc] initWithName:#"dawdw" andImage:#"dwddw" andDescription:#"dawdw" andTypes:#"dawwd dawwd" andforWho:#"dwaadw"]];
You need to init new array. You can do it like so:
NSMutableArray *newarray = [NSMutableArray array];
The problem here is you didn't initialize newarray. You need to do
newarray = [NSMutableArray array];
But also note that adding self.tableData[0] to it is different from adding another alloc'ed and init'ed Cell to it because the former increases the reference count on self.tableData[0], while the latter creates a new Cell object.
This also means that if you make a new Cell object and add it to the mutable array, and later on modified that object in the mutable array, it wouldn't change the cell in the first array. But if you did it the first way, it would.
NSMutableDictionary *expense_ArrContents = [[NSMutableDictionary alloc]init];
for (int i = 1; i<=4; i++) {
NSMutableArray *current_row = [NSMutableArray arrayWithObjects:#"payer_id",#"Expense_Type_id",#"Category_Id",#"SubCategory_Id",nil];
[expense_ArrContents setObject:current_row forKey: [NSNumber numberWithInt:i]];
}
NSArray *newArray = [expense_ArrContents allKeysForObject:#"payer_id"];
NSLog(#"%#",[newArray description]);
i want to get the list of key values containing the particular object which is in the array of values stored in nsmutabledictionary for a particular key.
In the line where you get all the keys ([expense_ArrContents allKeysForObject:#"payer_id"];) you actually get keys for an object that is not in any of the array's items. This #"player_id" is different object than the #"player_id" you added in current_row. In fact, maybe all of your rows have different #"player_id" objects (except if the compiler has made some optimization - maybe it threats that same string literal as one object instead of creating new object for each iteration).
Try creating an NSString object for the #"player_id" which you add to the current_row and then get all the keys for that same object:
NSString* playerId = #"player_id";
for(){
NSMutableArray *current_row = [NSMutableArray arrayWithObjects: playerId,...];
...
}
NSArray *newArray = [expense_ArrContents allKeysForObject:playerId];
Your NSArray *newArray = [expense_ArrContents allKeysForObject:#"payer_id"]; will not return any value because in expense_ArrContents there is no such key(#"payer_id"), instead there are keys like 1,2,3 etc.What is your requirement?Want to see what all keys are there in expense_ArrContents just log
NSArray*keys=[expense_ArrContents allKeys];
Try this :
NSMutableArray *array_key=[[NSMutableArray alloc]init];
for (NSString *key in expense_ArrContents) {
if ([[expense_ArrContents objectForKey:key] containsObject:#"payer_id"]) {
[array_key addObject:key];
}
}
I have a list of objects (trucks) with various attributes that populate a tableview. When you tap them they go to an individual truck page. There is an add button which will add them to the favorite list in another tableview. How do I initialize an empty mutable array in Cocoa?
I have the following code:
-(IBAction)addTruckToFavorites:(id)sender:(FavoritesViewController *)controller
{
[controller.listOfTrucks addObject: ((Truck_Tracker_AppAppDelegate *)[UIApplication sharedApplication].delegate).selectedTruck];
}
Update:
With new syntax you can use:
NSMutableArray *myArray = [NSMutableArray new];
Original answer:
for example:
NSMutableArray *myArray = [[NSMutableArray alloc] init];
And here you find out why (difference between class and instance method)
Basically, there are three options:
First
NSMutableArray *myMutableArray = [[NSMutableArray alloc] init];
Second
NSMutableArray *myMutableArray = [NSMutableArray new];
Third
NSMutableArray *myMutableArray = [NSMutableArray array];
You can also initialize in this way
Another way for Objective C apart from the answer of #NSSam
NSMutableArray *myMutableArray = [#[] mutableCopy];
For Swift
let myArray = NSMutableArray()
OR
let myArray = [].mutableCopy() as! NSMutableArray;
NSMutableArray *arr = [NSMutableArray array];
I use this way to initialize an empty mutable array in Objective C:
NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];
listOfTrucks = [NSMutableArray array]; gives you a new mutable array.
In modern Objective - C it could be even more shorter:
NSArray *array = #[];
I am making the switch from Java to Objective-c, and I'm having some difficulty. I have searched this problem this without much success.
I have an NSMutableArray that stores NSMutableArrays. How do I add an array to the array?
You can either store a reference to another array (or any type of object) in your array:
[myArray addObject:otherArray];
Or concatenate the arrays.
[myArray addObjectsFromArray:otherArray];
Both of which are documented in the documentation.
Since an array is just an object like any other:
[myContainerMutableArray addObject:someOtherArray];
Or if you want to concatenate them:
[myFirstMutableArray addObjectsFromArray:otherArray];
You add it like any other object.
NSMutableArray *innerArray = [NSMutableArray array];
NSMutableArray *outerArray = [NSMutableArray array];
[outerArray addObject:innerArray];
In case if you add the same NSMutableArray Object, Like
NSMutableArray *mutableArray1 = [[NSMutableArray alloc]initWithObjects:#"test1",#"test2",#"test3",nil];
NSMutableArray *mutableArray2 = [[NSMutableArray alloc]initWithObjects:#"test4",#"test5",#"test6", nil];
mutableArray1 = [NSMutableArray arrayWithArray:mutableArray1];
[mutableArray1 addObjectsFromArray:mutableArray2];
Nslog(#"mutableArray1 : %#",mutableArray1);
[YourArray addObjectsFromArray:OtherArray];