取出 NSArray 中的一些对象并将其随机放入标签中
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的一般方法在这里很好,但您的循环逻辑似乎有缺陷。我将清理一些东西,看看是否对您有帮助。
首先,让我们将标签放入自己的数组中,以便更容易处理。这将使我们在稍后创建循环时通过索引而不是通过变量名称访问每个标签。
现在让我们用您的短语创建一个可变数组 - 这可以使用循环来完成,因为您知道您要查找的键具有连续的数字:
现在我们需要创建一个循环来随机从可变字典中提取短语并分配它们到随机标签。我们将运行这个循环六次,每次选择一个随机短语并将其分配给一个标签。分配该短语后,我们会将其从词典中删除,这样就不会再次使用它。
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.
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:
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.