0

I been searching for this but failed to find a proper solution. Is it possible to create an array for each object present in an array?Lets say I have an array 'fruits'

   NSMutableArray *fruits=[[NSMutableArray alloc]init];
   [fruits addObject:@"apple"];
   [fruits addObject:@"banana"];
   [fruits addObject:@"mango"];

Now there are three objects in my array. IS it possible to create an array for each of the object present in the array 'fruit'.

Can I do something like

   for(int i=0;i<fruits.count;i++){
    NSMutableArray *fruits_%d=[[NSMutableArray alloc]init];
    }

I know it is a blunder. Is there any way I can do that?

Thanks in advance.

5
  • 1
    What do you want to do with these arrays? Most likely you want to create an array of these new arrays. Commented Oct 4, 2014 at 21:39
  • 1
    It sounds like you may be looking for a NSDictionary - you could store arrays with the fruit names as the keys Commented Oct 4, 2014 at 21:41
  • Lets say, I have an array called account and then I would like to store all the details of those accounts in another array created. @rmaddy Commented Oct 4, 2014 at 21:45
  • (But you can't do this: NSMutableArray *fruits_%d=[[NSMutableArray alloc]init];. If you want multiple fruits arrays, make it an array of arrays.) Commented Oct 4, 2014 at 21:47
  • I know it is a blunder. I used that just to give a clear understanding about my question. @HotLicks Commented Oct 4, 2014 at 21:54

2 Answers 2

2

You could use a dictionary instead:

NSMutableDictionary *fruitDict=[[NSMutableDictionary alloc]init];
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:@"apple"];
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:@"banana"];
[fruitDict setObject:[[NSMutableArray alloc]init] forKey:@"mango"];

Or a little cleaner syntax:

NSMutableArray *fruits=[[NSMutableArray alloc]init];
[fruits addObject:@"apple"];
[fruits addObject:@"banana"];
[fruits addObject:@"mango"];

NSMutableDictionary *fruitDict = [[NSMutableDictionary alloc] init];

for (NSString *fruit in fruits)
{
    [fruitDict setObject:[[NSMutableArray alloc]init] forKey:fruit];
}

Then when you want to retrieve the array:

NSMutableArray *myArray = fruitDict[@"banana"]

The above code will yield the array for the banana item.

Sign up to request clarification or add additional context in comments.

3 Comments

Instead of forKey:@"apple", Can I use forKey:[fruits objectatindex:0]; ?
@T_77 No, that would create a dictionary with only one key, @"apple". You could use your for(int i=0;i<fruits.count;i++) and forKey:[fruits objectAtIndex:i] though.
ya...thats what I am thinking. Let me check!!
1

I don't know what exactly you want but maybe you can do something like this:

NSMutableArray *fruits = [[NSMutableArray alloc] init];
[fruits addObject:@"apple"];
[fruits addObject:@"banana"];
[fruits addObject:@"mango"]; 

NSMutableArray *arrayOfFruits = [[NSMutableArray alloc] initWithCapacity:fruits.count];
for (int i = 0; i < fruits.count; i++) {
    [arrayOfFruits addObject:@[fruits[i]]];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.