NSMutableArray of NSMutableArrays - objective-c

I want to create an NSMutableArray of NSMutableArrays. Below is the psuedo code:
NSMutableArray *mapcoords = [[NSMutableArray alloc] init];
NSMutableArray *clustercoords = [[NSMutableArray alloc] init];
while (!FINISHED)
{
for(int j = 1;j <= 4;j++)
{
x = arc4random_uniform(45);
[mapcoords addobject:[NSNumber numberWitInt:x]];
}
[clustercoords addobject:mapcoords];
[mapcoords removeAllObjects];
}
When I inspect the "clustercoords" array at the end of the loop, all of the objects contain the same values. I figured this must be because I am pointing to the same object (mapcoords) every time I add it to clustercoords.
I assume that I would need a different several different "mapcoords" arrays so that the objects in mapccords are unique. Is this the correct assumption? Is there another alternative?

Your assumption is correct. Move the declaration and creation of mapcoords inside the while loop.
NSMutableArray *clustercoords = [[NSMutableArray alloc] init];
while (!FINISHED) {
NSMutableArray *mapcoords = [[NSMutableArray alloc] init];
for(int j = 1; j <= 4; j++) {
u_int32_t x = arc4random_uniform(45);
[mapcoords addObject:#(x)];
}
[clustercoords addObject:mapcoords];
}

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);
}

I need to use intermediate Array in loop

I have a basic question. I need to use intemediate array in loop. This is my code:
NSMutableArray *interArray = [[NSMutableArray alloc] initWithCapacity:2];
NSMutableArray *finalyArray = [[NSMutableArray alloc] init];
NSInteger a;
for(int j=0; j<5; j++)
{ [interArray removeAllObjects];
for(int i=0; i<2; i++)
{
a = [array ObjectAtInde:i]; // this is existing array
[interArray addObject:a];
}
[finalyArray addObject:interArray];
}
but when i delete objects in intermediate array I get empty Arrays in finaly Array. How to fix it? Thanks
The reason it won't work as you expect is because interArray is a reference to an object which you empty and fill in the loop and then add to the finalyArray, however you are adding the same intermediate array each time.
What you want to do it create a new intermediate array within the loop and therefore each intermediate array will be a separate object:
NSMutableArray *finalyArray = [[NSMutableArray alloc] init];
NSInteger a;
for(int j=0; j<5; j++)
{
NSMutableArray *interArray = [[NSMutableArray alloc] initWithCapacity:2];
for(int i=0; i<2; i++)
{
a = [array ObjectAtIndex:i]; // this is existing array
[interArray addObject:a];
}
[finalyArray addObject:interArray];
}
(Note this code won't work anyway as you cannot store NSInteger in a collection class without first wrapping it in a NSNumber object).

Selecting array elements using a binary pattern as the selector

I have an NSArray, and I've calculated a list of integers that represent (in binary) the elements I need to pull out of the array into a new array.
For example, I have 7, 11, and 13, whose bit patterns are 000111, 001011, and 001101. I want to grab three arrays, made of elements 0,1,2, then elements 0,1,3, and then 0,2,3 out of the main array.
Construct an NSIndexSet from the bit patterns you have:
#implementation NSIndexSet (NonContiguous)
+ (instancetype)indexSetFromMask:(NSUInteger)mask
{
NSMutableIndexSet * set = [NSMutableIndexSet indexSet];
for( NSUInteger i = 0; i < (sizeof(NSUInteger) * 8); i++ ){
if( mask & (1l << i) ){
[set addIndex:i];
}
}
return set;
}
#end
Then use objectsAtIndexes:
[origArray objectsAtIndexes:[NSIndexSet indexSetFromMask:7]];
// etc.
Assuming you want the output to be in the form [[a,b,c],[a,b,d],[a,c,d]] for the example, you could do something like this:
NSArray *sourceArray = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",#"d",...,nil];
NSArray *grabArray = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:7],[NSNumber numberWithInt:11],[NSNumber numberWithInt:13],...,nil];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
Then for each element of grabArray, add the correct elements to resultArray:
for (NSNumber num in grabArray) {
int n = [num intValue];
int bit = floor(log2(n)); //get highest bit in the current number
NSMutableArray *curr = [[NSMutableArray alloc] init];
while (n>0) {
if (n>pow(bit,2)) { //if this bit is a 1
[curr addObject:[sourceArray objectAtIndex:bit]];
}
n-=pow(bit,2);
bit-=1;
}
[resultArray addObject:curr];
}
Then resultArray should be the array you are looking for. It will add the objects in reverse order, so if order matters you would want to use [curr insertObject: [sourceArray objectAtIndex:bit] atIndex:0]; instead.

addObject replaces previous object in NSMutableArray

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);
}

Create multiple numbered variables based on a int

How would I create a number of NSDictionary variables using an array's count?
This is basically what I came up with, but I'm not sure how to make this work with Objective-C syntax. doesntContainAnother is an NSArray. I want the names of the dictionaries to use the current value of loopInt.
int *loopInt = 0;
while (doesntContainAnother.count <= loopInt) {
NSMutableDictionary *[NSString stringWithFormat:#"loopDictionary%i", loopInt] = [[[NSMutableDictionary alloc] init] autorelease];
[NSString stringWithFormat:#"loopDictionary%i", loopInt] = [NSDictionary dictionaryWithObject:[array1 objectAtIndex:loopInt]
forKey:[array2 objectAtIndex:loopInt]];
loopInt = loopInt + 1;
}
Create a mutable array and loop until you reach the original array's count, creating a dictionary and adding it to the mutable array on each iteration.
Your code should look like this.
NSMutableArray *dictionaries = [[NSMutableArray alloc] init];
for (int i = 0; i < doesntContainAnother.count; i++) {
[dictionaries addObject:[NSMutableDictionary dictionaryWithObject:[array1 objectAtIndex:i] forKey:[array2 objectAtIndex:i]]];
}
The approach of creating variables with numbers at the end of their names is an antipattern and not even possible in Objective-C. It's equivalent to an array, but clunkier.
You need to create a mutable array, and then put the objects into the array. You can't create a variable with the same name as the contents of a string, as you have done. For example:
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:[doesntContainAnother count]];
int i = 0; // Note: type is int, not int*
for (i = 0; i < [doesntCountainAnother count]; i++) {
[arr addObject:[NSMutableDictionary dictionary]];
}
// Later...
NSMutableDictionary *d1 = [arr objectAtIndex:3];
Or if you want to pull them out of the list by name:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:[doesntCountainAnother count]];
int i = 0;
for (i = 0; i < [doesntContainAnother count]; i++) {
[dict setObject:[NSMutableDictionary dictionary] forKey:[NSString stringWithFormat:#"loopDictionary%d", i]];
}
// Later...
NSMutableDictionary *d1 = [dict objectForKey:#"loopDictionary3"];
But the first way is likely the easiest.