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"]
Related
I want to add more data at 0th index of my array. There is already an array stored at 0th index of my array. Here is the line of code where data is stored at 0th index of my array.
if ([[albumDetailsResponse valueForKey:#"result"] isEqualToString:#"IsValidUser"]) {
[albumURLArray addObject:[albumDetailsResponse valueForKey:#"data"]];
}
Here, albumURLArray is a NSMutableArray and at its 0th index, the object will be stored. Now, I want to add more URL at 0th index of the same array. So, How can I achieve this?
Thanks in advance.
How to add object at first index of NSArray In this question's answer, the data is added at new index in the NSMutableArray while what I want is I want to add new data at 0th index of that array. So, my requirement is different then this question.
if([albumURLArray count]>0)
{
[albumURLArray removeAllObjects];
[albumURLArray addObject:[albumDetailsResponse valueForKey:#"data"]];
}
You can create one array and use that array as the 0th Index of Your albumURLArray
For e.g
NSMutableArray *arrtemp=[[NSMutableArray alloc]init];
[arrtemp addObject:[albumDetailsResponse valueForKey:#"data"]];
[arrtemp addObject:[albumDetailsResponse valueForKey:#"URL"]];
[albumURLArray addObject:arrtemp];
To get the URL,
Use [[albumURLArray objectAtIndex:0] objectAtIndex:1]]
Just use:
[albumURLArray insertObject:[albumDetailsResponse valueForKey:#"data"] atIndex:0]
[albumURLArray insertObject:[albumDetailsResponse valueForKey:#"data"] atIndex:0];
You can use-
[albumURLArray insertObject:[albumDetailsResponse valueForKey:#"data"] atIndex:0];
Instead adding array to 0th Index. Add a NSMutableDictionary. And add one key for response data and an NSArray for URL's.
NSMutableDictionary * dataDictionary = [NSMutableDictionary alloc] init];
dataDictionary setValue:[albumDetailsResponse valueForKey:#"data"] forKey:#"data"];
NSMutableArray * urlArray = #[[NSURl urlWithString:#"http://someurl.com"], [NSURl urlWithString:#"http://someurl1.com"]];
dataDictionary setValue:urlArray forKey:#"URLS"];
have already tried insetObject:atIndex: method but it didn't work.
The only three reasons it wouldn't work:
NSMutableArray isn't initialized. (i.e. albumURLArray = [#[] mutableCopy];or albumURLArray = [[NSMutableArray alloc] init];)
The index isn't correct (i.e. no item added yet but index 1 instead 0 used.)
The array is a plain NSArray
To add an object to the front of the array, use 0 as the index:
[albumURLArray insertObject:myObject atIndex:0];
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.
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 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];
How can I remove an object from a reversed NSArray.
Currently I have a NSMutableArray, then I reverse it with
NSArray* reversedCalEvents = [[calEvents reverseObjectEnumerator] allObjects];
now I need to remove at item from reversedCalEvents or calEvents and automatically refresh the table the array is displayed in based on conditions.
i.e.
if(someInt == someOtherInt){
remove object at index 0
}
How can I do this? I cannot get it to work.
Here's a more functional approach using Key-Value Coding:
#implementation NSArray (Additions)
- (instancetype)arrayByRemovingObject:(id)object {
return [self filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"SELF != %#", object]];
}
#end
You will need a mutable array in order to remove an object. Try creating reversedCalEvents with mutableCopy.
NSMutableArray *reversedCalEvents = [[calEvents reverseObjectEnumerator] allObjects] mutableCopy];
if (someInt == someOtherInt)
{
[reversedCalEvents removeObject:object];
}
NSArray is not editable, so that you cannot modify it. You can copy that array to NSMutableArray and remove objects from it. And finally reassign the values of the NSMutableArray to your NSArray.
From here you will get a better idea...
NSArray + remove item from array
First you should read up on the NSMutableArray class itself to familiarize yourself with it.
Second, this question should show you an easy way to remove the objects from your NSMutableArray instance.
Third, you can cause the UITableView to refresh by sending it the reloadData message.
you can try this:-
NSMutableArray* reversedCalEvents = [[[calEvents reverseObjectEnumerator] allObjects] mutableCopy];
[reversedCalEvents removeLastObject];