取出 NSArray 中的一些对象并将其随机放入标签中

发布于 2024-08-07 18:01:37 字数 2701 浏览 4 评论 0原文

我从 plist 中选择数组的索引。这是一本带有 10 行不同字符串的字典。

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

NSDictionary* phrase = [tableau objectAtIndex:nombreChoisi];

我想采用 6 个元素并将它们随机放入 6 个标签

我尝试将其传递到新的 NSMutableArray 中,但它不起作用:

    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];

我也尝试这样做:

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

但它给了我键而不是值,无法选择我需要的值。

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2024-08-14 18:01:37

您的一般方法在这里很好,但您的循环逻辑似乎有缺陷。我将清理一些东西,看看是否对您有帮助。

首先,让我们将标签放入自己的数组中,以便更容易处理。这将使我们在稍后创建循环时通过索引而不是通过变量名称访问每个标签。

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

现在让我们用您的短语创建一个可变数组 - 这可以使用循环来完成,因为您知道您要查找的键具有连续的数字:

// 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];
}

现在我们需要创建一个循环来随机从可变字典中提取短语并分配它们到随机标签。我们将运行这个循环六次,每次选择一个随机短语并将其分配给一个标签。分配该短语后,我们会将其从词典中删除,这样就不会再次使用它。

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文