How to add an NSMutableArray to an NSMutableArray Objective-c - ios

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];

Related

Retriving Array values from the NSMutableArray in IOS

I am working with an application in which i am getting photoID ,which is a string.
I am storing that photoID in array,and again add that array in another array.
Below iS the code::
NSString *photoID;
arr=[[NSMutableArray alloc]initWithCapacity:10];
array=[[NSMutableArray alloc] init];
[array addObject:photoID];
[arr arrayByAddingObjectsFromArray:array];
//number=(int)arr[1];
NSLog(#"arr : %#",arr);
NSLog(#"arr[0] : %#",arr[0]);
NSLog(#"arr[1] : %#",arr[1]);
NSLog(#"Number1 : %#",number1);
NSLog(#"Number : %d",number);
when i tried to access the value of arr[1],my application crashes.
i don't know what am i doing wrong.am i doing wrong to add strings in array,and truing to access unsaved data?
Please help me out.
Thanks in advance
It is because this line: [arr arrayByAddingObjectsFromArray:array]; does nothing to the arr, it only
Returns a new array that is a copy of the receiving array with the
objects contained in another array added to the end.
You should replace it with [arr addObjectsFromArray:array];. And also, you only have 1 element in arr which is at index 0, so the arr[1] should crash but arr[0] should work.
First array should be NSArray if you want to arrayByAddingObjectsFromArray or addObjectsFromArray
NSArray *array1=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSMutableArray *array2=[[NSMutableArray alloc]init];
[array2 addObjectsFromArray:array1];
NSLog(#"%d",array2.count);
You can also use like this:
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3", nil];
NSMutableArray *outerArray = [NSMutableArray array];
for(int i=0;i<=innerArray.count;i++)
{
[outerArray addObject:innerArray];
}

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];

How to Make Array of Float Data Type in ios

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.

How to initialize an empty mutable array in Objective C

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 = #[];

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