How to add an object to the end of an NSMutableArray - iphone

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.

Related

Can´t add objects to mutable array which returns nil

I´m doing an app and I can´t get a mutable array to accept objects. I´v tried setting breakpoints to see what´s happening but it keeps saying that the mutable array is nil. Does anyone has an answer?
My code:
- (void)save:(id) sender {
// All the values about the product
NSString *product = self.productTextField.text;
NSString *partNumber = self.partNumberTextField.text;
NSString *price = self.priceTextField.text;
NSString *quantity = self.quantityTextField.text;
NSString *weigh = self.weighTextField.text;
NSString *file = [self filePath];
//Singleton class object
Object *newObject = [[Object alloc] init];
newObject.product = product;
newObject.partNumber = partNumber;
newObject.price = price;
newObject.quantity = quantity;
newObject.weigh = weigh;
//Array declaration
mutableArray = [[NSMutableArray alloc]initWithContentsOfFile: file];
[mutableArray addObject:newObject];
[mutableArray writeToFile:file atomically:YES];
}
While initWithContentsOfFile: can be called on an NSMutableArray, it was inherited from NSArray. The return value is an NSArray which is not mutable. If you want to add objects to your mutable array, you have to do something like this:
mutableArray = [[[NSMutableArray alloc] initWithContentsOfFile: file] mutableCopy];
[mutableArray addObject:newObject];
[mutableArray writeToFile:file atomically:YES];
Now, the addObject: call should work.
Best regards.
[NSMutableArray initWithContentsOfFile:] returns nil by default if the file can't be opened or parsed. Are you sure the file you're loading exists and is formatted correctly?
Try to check with break point on
mutableArray = [[NSMutableArray alloc]initWithContentsOfFile: file];
Line. Move your cursor on mutableArray if it shows you __NSArrayI that means it is an immutable array i.e. you cant update it and if it shows you __NSArrayM that means it is a mutable array and you can update this array.
In your case you're getting immutable array thats why you cant update it.
So you have two way to get mutable Array from this file -
Method:1
mutableArray = [[[NSMutableArray alloc] initWithContentsOfFile: file] mutableCopy];
Method:2
NSArray *anyArray = [[NSArray alloc]initWithContentsOfFile: file];
mutableArray = [[NSMutableArray alloc]initWithArray:anyArray];
In both case mutableArray woud be a mutable Array. You can update it.

Rearrange NSArray and insert Object

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

Modifying an NSMutableArray inside a loop

I have an NSMutableArray filled with Task objects. I want to be able to delete those whose completed property are set to YES
NSMutableArray *allTasks = (NSMutableArray *)[[TaskStore defaultStore] allTasks];
NSMutableArray *completedTasks;
for (Task *task in allTasks) {
if ([task completed]) {
[completedTasks addObject:task];
}
}
[allTasks removeObjectsInArray:completedTasks];
While debugging I noticed that the completedTasks array is always empty. Why is this?
You forgot to initialize the completedTasks :
NSMutableArray *completedTasks = [NSMutableArray array];
You haven't initialized completedTasks. You need to add this:
NSMutableArray *completedTasks = [[NSMutableArray alloc] init];
You must initialize the array before you can use it, the initializing actually creates your array object -
To do this add This Line to create an autoreleased array (which means you dont have to release it)
NSMutableArray *completedTasks = [NSMutableArray array];
Or
NSMutableArray *completedTasks = [[NSMutableArray alloc] init];
But then you will have to release it by yourself [completedTasks release] when you are not using it any moere (unless you are using ARC).
This will create your array object.
Shani
from NSMutableArray documentation :
This method assumes that all elements in otherArray respond to hash and isEqual:.
You can try :
[allTasks filterUsingPredicate:[NSPredicate predicateWithFormat:#"completed = %#", [NSNumber numberWithBool:YES]]]

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

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