addObject to NSArray in Objective-C - ios

How to addObject to NSArray using this code? I got this error message when trying to do it.
NSArray *shoppingList = #[#"Eggs", #"Milk"];
NSString *flour = #"Flour";
[shoppingList addObject:flour];
shoppingList += #["Baking Powder"]
Error message
/Users/xxxxx/Documents/iOS/xxxxx/main.m:54:23: No visible #interface for 'NSArray' declares the selector 'addObject:'

addObject works on NSMutableArray, not on NSArray, which is immutable.
If you have control over the array that you create, make shoppingList NSMutableArray:
NSMutableArray *shoppingList = [#[#"Eggs", #"Milk"] mutableCopy];
[shoppingList addObject:flour]; // Works with NSMutableArray
Otherwise, use less efficient
shoppingList = [shoppingList arrayByAddingObject:flour]; // Makes a copy

You can't add objects into NSArray. Use NSMutableArray instead :)

Your array cant be changed because is defined as NSArray which is inmutable (you can't add or remove elements) Convert it to a NSMutableArray using this
NSMutableArray *mutableShoppingList = [NSMutableArray arrayWithArray:shoppingList];
Then you can do
[mutableShoppingList addObject:flour];

NSArray does not have addObject: method, for this you have to use
NSMutableArray. NSMutableArray is used to create dynamic array.
NSArray *shoppingList = #[#"Eggs", #"Milk"];
NSString *flour = #"Flour";
NSMutableArray *mutableShoppingList = [NSMutableArray arrayWithArray: shoppingList];
[mutableShoppingList addObject:flour];
Or
NSMutableArray *shoppingList = [NSMutableArray arrayWithObjects:#"Eggs", #"Milk",nil];
NSString *flour = #"Flour";
[shoppingList addObject:flour];

Related

can't add objects of an array in another array

i am trying to show selected cell checkmark when user is offline,but the array is not adding another array object, kindly help me
1.appDelegate.SelectedIDArray saving selected cell
2.buildingObject.selectedIDString saving checked cell index coma separated
//first i am removing all objects from array
[appDelegate.SelectedIDArray removeAllObjects];
//then i am adding string values in array(e.g 3,2,7)
NSMutableArray *tempArray = (NSMutableArray*)[buildingObject.selectedIDString componentsSeparatedByString:#","];
//now adding tempArray array objects in appDelegate.SelectedIDArray
[appDelegate.SelectedIDArray addObjectsFromArray:tempArray];
//now showing count of added object in appDelegate.SelectedIDArray
txtID.text=[NSString stringWithFormat:#"%zd of 23 selected",appDelegate.SelectedIDArray.count];
by saying offline, you mean app is terminated, then if you didn't save your array in NSUserDefaults it is nil already. then you should initialize it. if this is not the case another problem may be with appDelegate:
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *vals = #"1,2,3,4,5,6,7,8";
NSMutableArray *tempArray = [[vals componentsSeparatedByString:#","] mutableCopy];
if (!appDelegate.SelectedIDArray) {
appDelegate.SelectedIDArray = [NSMutableArray new];
}
else
{
[appDelegate.SelectedIDArray removeAllObjects];
}
[appDelegate.SelectedIDArray addObjectsFromArray:tempArray];
NSLog(#"%#", appDelegate.SelectedIDArray);
I've took sample test case similar to your code:
NSString *str1 = #"1,2,3,4,5,6,7,8,9";
NSMutableArray *tempArray = (NSMutableArray*)[str1 componentsSeparatedByString:#","];
NSMutableArray *arr = [NSMutableArray array];
[arr addObjectsFromArray:tempArray];
NSLog(#"%#",arr);
Its working fine for me. Make sure appDelegate.SelectedIDArray is NSMutableArray. Or if you want fresh array simply use
appDelegate.SelectedIDArray = [NSMutableArray arraywitharray:tempArr];
Hope this helps.
I hope you did not forget to alloc appDelegate.SelectedIDArray.
Hello buddy please try this
NSArray *tempArray = [buildingObject.selectedIDString componentsSeparatedByString:#","];
[appDelegate setSelectedIDArray:[tempArray mutableCopy]];
if it didnot work then possibly one of your array is empty.Please check
use it, it will fix if you are using the non arc
appDelegate.SelectedIDArray = [tempArray retain];

Creating NSArray from NSMutable Array

At the moment I'm creating an array as per below:
NSArray *data = #[#[#20, #40, #20, #60, #40, #140, #80],];
However I'd like to be able to create the same array from data already in an NSMutableArray.
I've tried this but the library I'm passing the array to does not like it.
NSArray *data = [NSArray arrayWithArray:self.altData];
("altData" is an NSMutableArray)
Any ideas?
An NSMutbaleArray is a subclass of an NSArray. So you can just do NSArray *immutable = [myMutableArray copy] or if you know that you're not going to change the mutable array after a point then you can do NSArray *immutable = mutableArray; maybe followed by mutableArray = nil. Your solution is also fine.

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 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 to add an NSMutableArray to an NSMutableArray Objective-c

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

Resources