views:

826

answers:

1

I choose index of an array from a plist. It's a dictionary with 10 different line with string.

    NSArray* tableau = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"codes" ofType:@"plist"]];

NSDictionary* phrase = [tableau objectAtIndex:nombreChoisi];

I would like to take 6 elements and put them in 6 labels randomly

I tried this to pass it in a new NSMutableArray, but it doesn't work:

    NSArray* tableau = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"codes" ofType:@"plist"]];

NSDictionary* phrase = [tableau objectAtIndex:nombreChoisi];

NSString *phraseMelange1 = [phrase objectForKey:@"texte1"] ;
NSString *phraseMelange2 = [phrase objectForKey:@"texte2"] ;
NSString *phraseMelange3 = [phrase objectForKey:@"texte3"] ;
NSString *phraseMelange4 = [phrase objectForKey:@"texte4"] ;
NSString *phraseMelange5 = [phrase objectForKey:@"texte5"] ;
NSString *phraseMelange6 = [phrase objectForKey:@"texte6"] ;

NSMutableArray *tableauMelange = [[NSMutableArray alloc] initWithObjects:
          phraseMelange1,
          phraseMelange2,
          phraseMelange3,
          phraseMelange4,
          phraseMelange5,
          phraseMelange6,
          nil];

for(int i=0;i<6;i++)
{
 int idx;
 idx = random()%[tableauMelange count];
 NSString* titre = [tableauMelange objectAtIndex:idx];
 texte1.text = titre;
 [tableauMelange removeObjectAtIndex:idx];
}
{
 int idx;
 idx = random()%[tableauMelange count];
 NSString* titre = [tableauMelange objectAtIndex:idx];
 texte2.text = titre;
 [tableauMelange removeObjectAtIndex:idx];
}
{
 int idx;
 idx = random()%[tableauMelange count];
 NSString* titre = [tableauMelange objectAtIndex:idx];
 texte3.text = titre;
 [tableauMelange removeObjectAtIndex:idx];
}
{
 int idx;
 idx = random()%[tableauMelange count];
 NSString* titre = [tableauMelange objectAtIndex:idx];
 texte4.text = titre;
 [tableauMelange removeObjectAtIndex:idx];
}
{
 int idx;
 idx = random()%[tableauMelange count];
 NSString* titre = [tableauMelange objectAtIndex:idx];
 texte5.text = titre;
 [tableauMelange removeObjectAtIndex:idx];
}
{
 int idx;
 idx = random()%[tableauMelange count];
 NSString* titre = [tableauMelange objectAtIndex:idx];
 texte6.text = titre;
 [tableauMelange removeObjectAtIndex:idx];
}
[tableauMelange release];

I also try this with :

    NSMutableArray *array;
array=[[phrase allKeys] mutableCopy];

But It gives me the keys and not the value without possibility to choose those I need.

Thanks for your help.

+2  A: 

Your general approach is good here, but your looping logic seems flawed. I'm going to clean a few things up and see if that helps you.

First of all let's put your labels into their own array, to make this easier to deal with. This will let us access each label by an index rather than by its variable name as we create our loop later.

NSArray *myLabels = [NSArray arrayWithObjects:texte1, texte2, texte3, texte4, texte5, texte6, nil];

Now let's create a mutable array with your phrases - this can be done using a loop because you know the keys you're looking for have sequential digits:

// This is your existing code that reads the phrases from a plist:
NSArray* tableau = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"codes" ofType:@"plist"]];
NSDictionary* phrase = [tableau objectAtIndex:nombreChoisi];

// Now read the phrases into a mutable dictionary:
NSMutableArray *myPhrases = [NSMutableArray arrayWithCapacity:6];
for (int i = 1; i <= 6; i++) {
  NSString *myPhrase = [phrase objectForKey:[NSString stringWithFormat:@"texte%d", i]];
  [myPhrases addObject:myPhrase];
}

Now we need to create a loop to randomly pull phrases out of the mutable dictionary and assign them to a random label. We'll run through this loop six times, each time picking a random phrase and assigning it to a label. After assigning the phrase, we'll remove it from the dictionary so it won't be used again.

for (int i = 0; i < 6; i++) {

  // Choose a random phrase
  int randIdx = random() % [myPhrases count];
  NSString *randPhrase = [myPhrases objectAtIndex:randIdx];

  // Assign the next label's text
  [myLabels objectAtIndex:i].text = randPhrase;

  // Remove the phrase from the mutable dictionary so it isn't used again:
  [myPhrases removeObjectAtIndex:randIdx];
}
pix0r
This help me, but I have an error "request for a member not a structure union" at : [myLabels objectAtIndex:i].text = randPhrase;Strange because, texte1.text = randPhrase; is OK.
MAGE