我有一个单词的文本文件,我可以在项目中阅读它的内容,现在,我想将单词字符串分成两半,并在我认为的多个标签上随机显示。
NSString *myfilePath = [[NSBundle mainBundle] pathForResource:@"textFile" ofType:@"txt"];
NSString *linesFromFile = [[NSString alloc] initWithContentsOfFile:myfilePath encoding:NSUTF8StringEncoding error:nil];
myWords = [NSArray alloc];
myWords = [linesFromFile componentsSeparatedByString:@"\n"];
NSLog(@"%@", myWords);
我做了5个标签,如何使文本文件中的单词拆分并随机出现在这5个标签上?
最佳答案
您可以尝试使用此
NSMutableArray *words = [myWords mutableCopy];
for (int i = 0; i<5 ;i++)
{
NSInteger index = arc4random()%words.count;
labels[i].text = words[index];
[words removeObjectAtIndex:index];
}
关于ios - 如何在 objective-c 中的多个标签上显示文本文件中的随机单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42834395/