Add other object to NSArray - ios

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

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

addObject to NSArray in Objective-C

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

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.

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