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];
Related
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];
i have an array and i get value with this codes:
NSString *status = [[Sweetresponse objectAtIndex:path.row] objectAtIndex:9];
i wanna change this value :
[[Sweetresponse objectAtIndex:path.row] replaceObjectAtIndex:9 withObject:#"liked"];
But its not working because this structure is array in array. how can i fix this problem ?
This is because the NSArray is immutable. You have to make it mutable.
NSMutableArray *mutableResponse = [Sweetresponse mutableCopy];
NSMutableArray *mutableResponseItems = [[mutableResponse objectAtIndex:path.row] mutableCopy];
// replace at the index
[mutableResponseItems replaceObjectAtIndex:9 withObject:#"liked"];
// create immutables and replace it with our new array
mutableResponse[path.row] = [mutableResponseItems copy];
// set `Sweetresponse` (assuming it is an NSArray)
Sweetresponse = [mutableResponse copy];
edit: I have no idea what Sweetresponse really is, here I'm assuming it's a NSArray
#trojanfoe has a point with parsing this response into custom objects would lead to cleaner code and also simpler object modification.
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];
}
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"]
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];