I am trying to add NSMutableArray in another NSMutableArray. But what I am trying to do is nested arrays.
My current code is:
NSMutableArray *array1 = [NSMutableArray arrayWithObjects: #"Red", #"Green", #"Blue", #"Yellow", nil];
NSMutableArray *array2 =[[NSMutableArray alloc] init];
[array2 addObject:array1];
This code is adding 4 objects in array2 but I want it to add array1 as single object.
Edit: This code is working I know but in my case in XCode something is wrong with initializing and it is adding 4 objects. I still could not figure it out. So this piece of code is working properly. So the problem was about initialization in a for loop.
I copy/pasted your code, and it adds one object to array2, not four.
Printing description of array2:
<__NSArrayM 0xc46c7b0>( <-- THIS ARRAY HAS 1 OBJECT
<__NSArrayM 0xc488770>( <-- THIS ARRAY HAS 4 OBJECTS
Red,
Green,
Blue,
Yellow
)
)
You may be are getting confused by the fact that printing the description, prints the contents of the inner array also.
Try:
NSMutableArray *array2 =[[NSMutableArray alloc] initWithArray:array1];
i am using this and its working
NSArray *array1 = #[array2, array3, ...];
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 will be putting a variety of things in this mutable array, but first I am just trying to make sure it works by putting in strings, and then pulling out the strings. Here is my code
str1=#"1";
str2=#"2";
str3=#"3";
NSMutableArray *testArray;
[testArray addObject:str1];
[testArray addObject:str2];
[testArray addObject:str3];
retrieve =[testArray objectAtIndex:1];
NSLog(#"the test number is %#",retrieve);
The problem is that my string:retrieve equals "null" after receiving the string from the array. I'm not sure what I am doing wrong, I've looked at Apple's documentation but I'm having trouble making sense of it. I know I must be interacting with the array incorrectly, but I'm not sure how exactly. Help will be appreciated.
-Thank you!
You did not initialize your testArray:
NSMutableArray *testArray = [NSMutableArray array];
You can populate the array using new syntax. If you needed mutability only to add the three items, you could use a non-mutable array instead, like this:
NSArray *testArray = #[ #"1", #"2", #"3"];
If you do need mutability, call mutableCopy:
NSMutableArray *testArray = [#[ #"1", #"2", #"3"] mutableCopy];
Well, that's because testArray is nil. you should change the 4th line to
NSMutableArray *testArray = [NSMutableArray array];
You array is nil.
You are missing
NSMutableArray *testArray = [NSMutableArray array];
To clarify what others are telling you:
This line
NSMutableArray *testArray;
Does not create an array. It creates a pointer variable that can be used to point to a mutable array. It starts out containing a zero value (nil, points to nothing.)
It's like a postal address that points to an empty lot.
You need to create (allocate) and initialize a mutable array object in order to use it. (Continuing our analogy, you have to build a house and put a mailbox in front of it before the address becomes valid.)
So you need to say:
testArray = [#[ #"1", #"2", #"3"] mutableCopy];
Breaking that down:
The inner part,
#[ #"1", #"2", #"3"]
Creates an immutable array that contains 3 string objects.
Then we ask the immutable array to create a mutable copy of itself. We save the address of that newly created mutable array into the pointer variable testArray.
We could do it in 3 steps:
NSMutableArray *testArray;
NSArray *tempArray = #[ #"1", #"2", #"3"];
testArray = [tempArray mutableCopy];
Or all at once, like #dasblinkenlight's code:
NSMutableArray *testArray = [#[ #"1", #"2", #"3"] mutableCopy];
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]];
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];