NSMutableArray not retaining? - objective-c

I'm having some issues with my Arrays, I hoped anyone here can tell me what I'm doing wrong.
I have 3 arrays: tmpAnswersArray, localAnswersArray & finalAnswersArray.
I also have a method to shuffle a given array in the same class.
The first NSLog shows the count for the localAnswersArray is 6 (how it supposed to be).
The third NSLog shows the localAnswersArray count is 0, even though I didn't change anything in that array.
localAnswersArray = [[NSMutableArray alloc] init];
localAnswersArray = [self shuffleArray:tmpAnswersArray];
[tmpAnswersArray release];
NSLog(#"Shuffled localAnswersArray (count = %d) & removed tmpAnswersArray",[localAnswersArray count]);
finalAnswersArray = [[NSMutableArray alloc] init];
NSLog(#"init finalAnswersArray");
for (int arrayCount = 0; arrayCount < 6; arrayCount++) {
NSLog(#"TEST ---> %d",[localAnswersArray count]);
[finalAnswersArray addObject:[localAnswersArray objectAtIndex:arrayCount]];
}
Is the first line of this code also retains the array?
Do I have to retain it manually somehow?
Why is the array count drops to 0 all of a sudden?
Thanks for any help!

You need to review the Memory Management Guidelines.
localAnswersArray = [[NSMutableArray alloc] init];
localAnswersArray = [self shuffleArray:tmpAnswersArray];
You create an array (that you own), then immediately replace it with a different array.
And it would seem that your entire block of code could be replaced with this:
finalAnswersArray = [[self shuffleArray:tmpAnswersArray] mutableCopy];

Related

Unable to add multiple objects to NSArray [duplicate]

I'm trying to add objects to a NSMutableArray through a for loop. But it seems whenever I add an object it replaces the old one so that I only have one object in the array at the time...
Do you have any idea of what might be wrong?
- (void)viewDidLoad
{
[super viewDidLoad];
LoginInfo *info = [[LoginInfo alloc] init];
info.startPost = #"0";
info.numberOfPosts = #"10";
info.postType = #"1";
getResults = [backendService getAllPosts:info];
for (NSInteger i = 0; i < [getResults count]; i++) {
Post *postInfo = [[Post alloc] init];
postInfo = [getResults objectAtIndex:i];
dataArray = [[NSMutableArray alloc] init];
[dataArray addObject:postInfo.noteText];
NSLog(#"RESULT TEST %#", dataArray);
}
}
It's the RESULT TEST log that always shows only the last added string in the output.
you are initialising the dataArray inside the for loop, so everytime it is created again (which means there are no objects) and a new object is added
move
dataArray = [[NSMutableArray alloc] init];
to before the for loop
also there is no need to alloc/init the postInfo object when you immediately override it with the object from the getResults array
You keep re-initializing the array for every run of the loop with this line:
dataArray = [[NSMutableArray alloc] init];
So dataArray is set to a new (empty) array for every run of the loop.
Initialize the array before the loop instead. Try something like this:
dataArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [getResults count]; i++) {
PostInfo *postInfo = [getResults objectAtIndex:i];
[dataArray addObject:postInfo.noteText];
NSLog(#"RESULT TEST %#", dataArray);
}

Trying to build polygon from NSString

So, I'm trying to build an array of CGPoints by breaking an NSString, typically look like this:
31.241854,34.788867;31.241716,34.788744;31.242547,34.787585;31.242661,34.787719
Using this code:
- (NSMutableArray *)buildPolygon:(NSString *)polygon
{
NSMutableArray *stringArray = [[NSMutableArray alloc] init];
[stringArray addObject:[polygon componentsSeparatedByString:#";"]];
NSMutableArray *polygonArray = [[NSMutableArray alloc] init];
for (int i=0; i < polygonArray.count; i++)
{
NSArray *polygonStringArray = [[NSArray alloc] init];
polygonStringArray = [[stringArray objectAtIndex:i] componentsSeparatedByString:#","];
CGFloat xCord = [[polygonStringArray objectAtIndex:0] floatValue];
CGFloat yCord = [[polygonStringArray objectAtIndex:1] floatValue];
CGPoint point = CGPointMake(xCord, yCord);
[polygonArray addObject:[NSValue valueWithCGPoint:point]];
}
NSLog(#"return polygonArray: %#", polygonArray);
return polygonArray;
}
But eventually I get an empty array.
What I'm doing wrong?
You're defining polygonArray as an empty array just before the start of your for loop. You should define polygonArray like:
NSArray *polygonArray = [polygon componentsSeparatedByString:#";"];
And you don't even need to bother with that stringArray variable.
You have confusion over alloc & init, and one simple typo...
The confusions first:
NSMutableArray *stringArray = [[NSMutableArray alloc] init];
This creates a new NSMutableArray and stores a reference to it in stringArray. All good so far.
[stringArray addObject:[polygon componentsSeparatedByString:#";"]];
And this obtains a reference to an NSArray ([polygon componentsSeparatedByString:#";"]) and adds it as a single element to the mutable array referenced by stringArray. There is nothing wrong per se with this, but it is not what you want in this case - you just want the array returned by componentsSeparatedByString:. You do this with:
NSArray *stringArray = [polygon componentsSeparatedByString:#";"];
Which takes the reference returned by componentsSeparatedByString: and stores it in the variable stringArray - no alloc or init required as you are not creating the array yourself. You don't even own this array, so if you are using MRC there is no need to release it later.
NSArray *polygonStringArray = [[NSArray alloc] init];
Now this allocates an immutable empty array and stores a reference to it in polygonStringArray. This is not a very useful array, as it contains nothing and cannot be modified! But you don't keep it around long...
polygonStringArray = [[stringArray objectAtIndex:i] componentsSeparatedByString:#","];
This obtains a reference to an array from componentsSeparatedByString: and stores it in polygonStringArray. If you are using MRC this will cause a leak - your pointless zero-length array created above will leak, and a new zero-length array will be created and leaked every time around the loop.
You are confused over allocation - you only need to allocate things you are creating; when you receive a reference to an already allocated object you only need to store that reference. (If using MRC you may also need to retain/release/autorelease it as well - but let's stick with ARC.) So all you needed here was:
NSArray *polygonStringArray = [[stringArray objectAtIndex:i] componentsSeparatedByString:#","];
Now your code is almost correct, just one typo:
for (int i=0; i < polygonArray.count; i++)
Well you are filling polygonArray in this loop and it starts off as empty, what you need is stringArray.count.
HTH

Adding NSMutableArray to another from a loop seems to create duplicates

I'm parsing through an NSDictionary of json-encoded events and placing them into a two-dimensional NSMutableArray based on their month -- for display in a sectioned table view.
Since I am adding items to an array and then placing that array in an array (event_container) in a loop, event_container shows the correct number of arrays, however, they all appear to be duplicates of the last iteration, so all of the contents of event_container are the same array.
I believe this is because it's a pointer and/or not being released. I'm unsure of an appropriate way around this or possibly even a better solution. I'm using ARC.
int month = 0;
int current_month = 0;
int counter = 0;
event_container = [[NSMutableArray alloc] init];
temp_array = [[NSMutableArray alloc] init];
for (NSDictionary *result in results)
{
NCEvent *anEvent = [[NCEvent alloc] init];
anEvent.title = [result objectForKey:#"title"];
anEvent.startdate = [result objectForKey:#"startdate"];
anEvent.enddate = [result objectForKey:#"enddate"];
NSDateFormatter *importDate = [[NSDateFormatter alloc] init];
[importDate setDateFormat:#"yyyy-M-d H:m:ss"];
anEvent.dateStart = [importDate dateFromString:anEvent.startdate];
anEvent.dateEnd = [importDate dateFromString: anEvent.enddate];
NSDateFormatter *exportDate = [[NSDateFormatter alloc] init];
[exportDate setDateFormat:#"d"];
anEvent.text_date = [exportDate stringFromDate: anEvent.dateStart];
NSDateFormatter *exportMon = [[NSDateFormatter alloc] init];
[exportMon setDateFormat:#"MMM"];
anEvent.text_mon = [exportMon stringFromDate: anEvent.dateStart];
NSDateFormatter *monthInt = [[NSDateFormatter alloc] init];
[monthInt setDateFormat:#"M"];
month = [[monthInt stringFromDate: anEvent.dateStart] intValue];
if(counter == 1){ //first month
current_month = month;
NSLog(#"I'm the first month: %i", month);
[temp_array addObject:anEvent];
}
else if(month > current_month){ //new month
NSLog(#"This is a new month");
current_month = month;
//add the events array to events container and reset the events array
[self.event_container addObject: temp_array];
[temp_array removeAllObjects];
[temp_array addObject:anEvent];
}
else{
NSLog(#"Same Month"); //same month
[temp_array addObject:anEvent];
}
NSLog(#"Event month integer: %i", month);
anEvent = nil;
counter++;
}
Those arrays are declared as properties:
#property (nonatomic, retain) NSMutableArray *event_container;
#property (nonatomic, retain) NSMutableArray *temp_array;
In the line:
[self.event_container addObject: temp_array];
You are always adding the same instance temp_array to self.event_container. This is why you see the same array duplicated many times.
You can solve this by doing the following for example:
-Add the following before your for loop
for (int i = 0; i < 12; i++) {
[event_container addObject:[NSMutableArray array]];
}
for (NSDictionary *result in results)
...
-Remove
if(counter == 1){ //first month
current_month = month;
NSLog(#"I'm the first month: %i", month);
[temp_array addObject:anEvent];
}
-and change the code that comes after that into :
tmp_array = [event_container objectAtIndex:month];
[temp_array addObject:anEvent];
Your suspicions about the array being a pointer is basically correct. The problem is that your temp_array isn't so temporary -- it's in fact the same array object every time through your loop.
You're creating it outside the loop, and whenever you send it addObject: or removeAllObjects, it's affecting the stuff that you've already put in there.
The key part, though, is that when you add the temp_array to event_container, it's the exact same object. It's not copied; the event_container array just gets a pointer to temp_array. When you add it again, it's the same thing. Since event_container just holds a whole bunch of pointers, you end up looking at the same object when you inspect it.
That's what's happening. To solve this, you need to create a separate array for each month; I think that sch's answer will work for you.
A quick demonstration:
NSMutableArray * container = [NSMutableArray array];
NSMutableArray * temp = [NSMutableArray array];
int i;
for( i = 0; i < 5; i++ ){
[temp addObject:[NSNumber numberWithInt:i]];
[container addObject:temp]; // Doesn't copy; just adds pointer to temp
[temp removeAllObjects];
}
// Inspecting container now, we find that it has five arrays, all empty.
NSLog(#"%#", container);
temp_array is a pointer type (like all objects in objective c). Therefore, with this call:
[self.event_container addObject: temp_array];
...you are adding a pointer to that object to event_container. You are not creating a new array, merely adding multiple pointers to the same object. What you most likely want to do is add a (pointer to a) copy of the object, like this:
[self.event_container addObject: [temp_array mutableCopy]];

Size of the NSMutable Array in objective C?

I want to ask the size of the NSMutable Array can be 2000? If not, is it possible to open an array to store 2000 elements. The elements is the user-defined object. Thank you.
The answer is that an NSMutableArray always starts with a size of zero. If you want it to be so you can do something like this:
NSMutableArray* anArray = [NSMutableArray arrayWithSize: 2000];
[anArray replaceObjectAtIndex: 1999 withObject: foo];
you need to prefill the array with NSNull objects e.g.
#implementation NSArray(MyArrayCategory)
+(NSMutableArray*) arrayWithSize: (NSUInteger) size
{
NSMutableArray* ret = [[NSMutableArray alloc] initWithCapacity: size];
for (size_t i = 0 ; i < size ; i++)
{
[ret addObject: [NSNull null]];
}
return [ret autorelease];
}
#end
Edit: some further clarification:
-initWithCapacity: provides a hint to the run time about how big you think the array might be. The run time is under no obligation to actually allocate that amount of memory straight away.
NSMutableArray* foo = [[NSMutableArray alloc] initWithCapacity: 1000000];
NSLog(#"foo count = %ld", (long) [foo count]);
will log a count of 0.
-initWithCapacity: does not limit the size of an array:
NSMutableArray* foo = [[NSMutableArray alloc] initWithCapacity: 1];
[foo addObject: #"one"];
[foo addObject: #"two"];
doesn't cause an error.

Fill NSMutableArray from another NsMutableArray

I have two classes indexViewController and flashCardQuestionViewController.
In the indexViewController i have table filled with an array.
Now i am getting some data from the database:
-(void)getMultipleChoiceAnswer
{
if(optionid!=nil)
[optionid removeAllObjects];
else
optionid = [[NSMutableArray alloc] init];
if(optionText!=nil)
[optionText removeAllObjects];
else
optionText = [[NSMutableArray alloc] init];
clsDatabase *clsDatabaseObject = [[clsDatabase alloc] init];
sqlite3_stmt *dataRows = [clsDatabaseObject getDataset:"select optionID,OptionText from flashCardMultipleAnswer where questionId=1"];
while(sqlite3_step(dataRows) == SQLITE_ROW)
{
[optionid addObject:[NSNumber numberWithInt:sqlite3_column_int(dataRows,0)]];
[optionText addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(dataRows,1)]];
}
sqlite3_finalize(dataRows);
[clsDatabaseObject release];
}
and I am calling this method in the viewDidLoad method of the indexViewController.
Now I have another NSMutableArray in the flashCardQuestionViewController named listNoOfOptionsInQuestion.
I want to fill listNoOfOptionsInQuestion with objects from optionText array in indexViewController.
How can I do this?
There are a number of ways to copy arrays: you can either use -[NSArray copy] to get an immutable copy, or -[NSArray mutableCopy] for a mutable copy. Don't forget that copy adds a reference so you'll need a release or autorelease somewhere (if you're not using GC that is).
Alternatively, you can use -[NSMutableArray addObjectsFromArray:].
Given your example, it looks like you want to do something like this at the end:
[flashCardQuestionViewController setListNoOfOptionsInQuestion:optionText];
And then in FlashCardQuestionViewController, you want something like:
- (void)setListNoOfOptionsInQuestion:(NSArray *)options
{
if (options != listNoOfOptionsInQuestion) {
[listNoOfOptionsInQuestion release];
listNoOfOptionsInQuestion = [options mutableCopy];
}
}
Rahul,
Do you really need to have a completely different copy of the MutableArray in each object. Would it be possible to have both objects point to the same array? For instance:
ClassOne *one = [[ClassOne alloc] init];
ClassTwo *two = [[ClassTwo alloc] init];
// build mutable array mArray
// ...
one.objectArray = mArray;
two.objectArray = mArray;
Or do you need to make changes to the two arrays in different ways? The try this (as suggested by Chris above) :
ClassOne *one = [[ClassOne alloc] init];
ClassTwo *two = [[ClassTwo alloc] init];
// build mutable array mArray
// ...
one.objectArray = mArray;
two.objectArray = [mArray mutableCopy];
again, if this isn't what you need then you'll have to give us a more precise question or problem that we can identify.