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.
Related
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..
I want to make array of float type.
Anybody can help me?
NSArray *arrOfFloat = [[[NSArray alloc]initWithObjects:[12.2, 23.44], nil]];
But i want to make array dynamically.
But i want to make array dynamically.
This means you'll have to use an NSMutableArray.
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:#1.1];
[array addObject:#2.2];
...
You also can't add primitives to an array. You'll need to add objects. Notice the # I added before the numbers. This creates number literals.
If you have the floats you want to add as variables, you can auto boxing like this:
[array addObject:#(myFloatVariable)];
Use NSMutableArray class instead of a NSArray (this is a subclass of it), this way, within your code, you will be able to call :
NSMutableArray *yourArray = [NSMutableArray new];
[yourArray addObject:#(1.0f)];
NSArraycan only store objects, so in your case you would have to store your float as NSNumber. If you want to store objects dynamically, thus adding or removing them to an NSArray you have to use the mutable object type called NSMutableArray.
You'll need to wrap your float's in an NSNumber:
[NSNumber numberWithFloat:12.2];
If you're dynamically adding elements to an array, you need to use NSMutableArray.
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSNumber numberWithFloat:12.2]];
You can use NSMutableArray for example named arrOfFloat and add this :
[arrOfFloat addObject:[NSNumber numberWithFloat:3.5]];
[arrOfFloat addObject:[NSNumber numberWithFloat:23.44]];
Hope, It will may helpful to you.
I want to add #"ALL ITEMS" object at the first index of NSARRAY.
Initially the Array has 10 objects. After adding, the array should contains 11 objects.
you can't modify NSArray for inserting and adding. you need to use NSMutableArray. If you want to insert object at specified index
[array1 insertObject:#"ALL ITEMS" atIndex:0];
In Swift 2.0
array1.insertObject("ALL ITEMS", atIndex: 0)
First of all, NSArray need to be populated when it is initializing. So if you want to add some object at an array then you have to use NSMutableArray. Hope the following code will give you some idea and solution.
NSArray *array = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"0", nil];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
[mutableArray addObject:#"ALL ITEMS"];
[mutableArray addObjectsFromArray:array];
The addObject method will insert the object as the last element of the NSMutableArray.
I know that we have six answers for insertObject, and one for creating a(n) NSMutableArray array and then calling addObject, but there is also this:
myArray = [#[#"ALL ITEMS"] arrayByAddingObjectsFromArray:myArray];
I haven't profiled either though.
Take a look at the insertObject:atIndex: method of the NSMutableArray class.To add an object to the front of the array, use 0 as the index:
[myMutableArray insertObject:myObject atIndex:0];
NSArray is immutable array you can't modify it in run time. Use NSMutableArray
[array insertObject:#"YourObject" atIndex:0];
NSArray is immutable but you can use insertObject: method of NSMutableArray class
[array insertObject:#"all items" atIndex:0];
As you are allready having 10 objects in your array,and you need to add another item at index 11...so,you must try this.... hope this helps..
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithCapacity:11];
[yourArray insertObject:#"All Items" atIndex:0];
NSArray is not dyanamic to solve your purpose you have to use NSMutableArray. Refer the following method
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
Apple documents says NSMutableArray Methods
[temp insertObject:#"all" atIndex:0];
Swift 3:
func addObject(){
var arrayName:[String] = ["Name1", "Name2", "Name3"]
arrayName.insert("Name0", at: 0)
print("---> ",arrayName)
}
Output:
---> ["Name0","Name1", "Name2", "Name3"]
I have an NSMutableArray (detailsMA)having 100 strings like #"True".
Am storing that array in NSMutableDictionary which is already allocated.
NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA];
[detailsDictionary setObject: array1 forKey:#"detailsMA"];
Now i changed the values of detailsMA, like replacing strings to #"False", After that am retrieving the original array values,
[detailsMA removeAllObjects];
[detailsMA addObjectsFromArray:[detailsDictionary objectForKey:#"detailsMA"]];
Here am getting modified values, not the original values.
I need original values.
Please help me.
You should copy the array.
NSArray *array1 = [[NSArray alloc] initWithArray:detailsMA copyItems:YES];
Don't forget to implement the NSCopying protocol in the classes for the objects to be copied
EDIT:
When you add the objects again, make a copy before adding.
[detailsMA addObjectsFromArray:[[NSArray alloc] initWithArray:[detailsDictionary objectForKey:#"detailsMA"] copyItems:YES]];
Let's say I have an NSArray of NSDictionaries that is 10 elements long. I want to create a second NSArray with the values for a single key on each dictionary. The best way I can figure to do this is:
NSMutableArray *nameArray = [[NSMutableArray alloc] initWithCapacity:[array count]];
for (NSDictionary *p in array) {
[nameArray addObject:[p objectForKey:#"name"]];
}
self.my_new_array = array;
[array release];
[nameArray release];
}
But in theory, I should be able to get away with not using a mutable array and using a counter in conjunction with [nameArray addObjectAtIndex:count], because the new list should be exactly as long as the old list. Please note that I am NOT trying to filter for a subset of the original array, but make a new array with exactly the same number of elements, just with values dredged up from the some arbitrary attribute of each element in the array.
In python one could solve this problem like this:
new_list = [p['name'] for p in old_list]
or if you were a masochist, like this:
new_list = map(lambda p: p['name'], old_list)
Having to be slightly more explicit in objective-c makes me wonder if there is an accepted common way of handling these situations.
In this particular case Cocoa is not outdone in succinctness :)
NSArray *newArray = [array valueForKey:#"name"];
From the NSArray documentation:
valueForKey:
Returns an array containing the
results of invoking valueForKey: using
key on each of the receiver's objects.