Rearrange NSArray and insert Object - iphone

I have an NSMutableArray which has five objects in it. I want to insert an object in the middle of an array, let me say atIndex 1, and move all the objects to index+1 how do I do that?
Can you please help me?

Use insertObject
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr insertObject:#"OBJ" atIndex:1];

With insertObject:AtIndex: method of NSMutableArray.
See docs.

NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
[arr insertObject:#"2" atIndex:[arr count]/2];

Related

Add dynamically created string to an array

I am making an iphone app in which I want to store the dynamically selected time into an array, but unable to implement the method to store the strings into an array. Following is the code which I am using but it is not giving the output.
- (void)storetimeintoanarray:(id)sender
{
NSDateFormatter *df3 = [[NSDateFormatter alloc] init];
[df3 setDateFormat:#"hh:mm:ss"];
timestr = [NSString stringWithFormat:#"%#",[df3 stringFromDate:objtimepicker.date]];
NSLog(#"time is:%#",timestr);
test = [[NSArray alloc]init];
[test arrayByAddingObject:timestr];
NSLog(#"array time:%#",test);
}
You have to declare array mutable object.
test = [[NSMutableArray alloc]init];
[test addObject:timestr];
You have to assign the result of the arrayByAddingObject: method to a new array like:
NSArray *newone = [test arrayByAddingObject:timestr];
After allocating you shouldn't allocate the array again. arrayByAddingObject returns a auto released new array. Also use a NSMutableArray when you want to add objects dynamically.
Change the code to
test = [[NSMutableArray alloc]init];
[test addObject:timestr];
You should be using NSMutableArray if you want to change it after creation.
NSMutableArray* arr = [[NSMutableArray alloc] init];
[arr addObject:timestr];
To create an array with a single object, you can use:
NSArray* arr = [NSArray arrayWithObject:timestr];
You should use NSMutableArray and its method addObject: instead of NSArray. [test arrayByAddingObject:timestr]; does nothing with your array test, its create new array

Select an array from an array of arrays to display in uitableview (iphone)

I have an array called addArray which I am adding array objects to:
NSMutableArray *addArray = [NSMutableArray array];
[addArray addObjectsFromArray: delegate.arrayobjectOne];
[addArray addObjectsFromArray: delegate.arrayobjectTwo];
// etc...
Now, if I only want to init one of these arrays to display in my table (preferably from another view controller but that's another question), how would I do this? And how would I access a specific property of each array object, e.g. arrayobjectOne.info?
Thanks for your time.
you can say
someObject = [addArray objectAtIndex: someIndex];
use this Example
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];

how to split string into NSMutableArray

I want to split string into NSMutableArray
I know
- (NSArray *)componentsSeparatedByString:(NSString *)separator
is available but this one is for NSArray not for NSMutableArray.
I need because after spliting i want to remove element from array by using
-(void)removeObjectAtIndex:(NSUInteger)index
which is not possible with NSArray.
Thank you
You can also just get a mutable copy of the returned array:
NSMutableArray *array = [[myString componentsSeparatedByString:#"..."] mutableCopy];
Also, remember that copy, like alloc, does allocate new memory. So when used in non-ARC code you must autorelease the copied array or manually release it when you are done with it.
Make a new NSMutableArray using
NSArray *array = [NSString componentsSeparatedByString:#"..."];
NSMutableArray *mutable = [NSMutableArray arrayWithArray:array];
Create a NSMutableArray from the output NSArray created by componentsSeparatedByString.
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:array];

nsmutablearray - Componentsseperatedbystring

How wouold i do this with a nsmutablearray, its simple with a nsarray just not a mutable array
Thanks
Just create mutable array from non-mutable that you get from that message:
NSMutableArray *mutableComponents = [NSMutableArray arrayWithArray:[yourString componentsSeparatedByString:...]];
The following will work too:
NSMutableArray *mArray = (NSMutableArray *)[yourString
componentsSeparatedByString:#"separator"];
You might need to retain and release when you're done.
Use the following code.
NSArray *array = [someString componentsSeparatedByString:#" "];
NSMutableArray *mutArray = [[NSMutableArray alloc] initWithArray:array];
use the mutArray, this is one NSMutableArray object.

How to add an object to the end of an NSMutableArray

I have the following code
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:filepath];
[plistArray insertObject:title atIndex:2];
but would like to rather add the object to the end of the array.
How can this be done, and do I need to set the last value to nil, at the moment I dont and it works fine.
Regards
[plistArray addObject:title];
See the NSMutableArray Class Reference.