Create multiple numbered variables based on a int
Asked Answered
A

2

4

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;
}
Appetency answered 9/2, 2010 at 19:17 Comment(1)
Can you clarify what you want? You want to create an array of dictionaries that each contain only one value?Trifurcate
A
4

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.

Assibilate answered 9/2, 2010 at 19:24 Comment(2)
Yeah, that makes sense. I updated my question with code I came up with. I just don't know how to set the object as a variable name. :/Appetency
That is exactly what I was trying to do. I had it in my head but I'm still too new to objective-c, and to be honest I don't think I've ever done that in C++.Appetency
R
2

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.

Router answered 9/2, 2010 at 19:39 Comment(1)
Thank you for explaining this. I understand this much better now :)Appetency

© 2022 - 2024 — McMap. All rights reserved.