我正在尝试将文件的内容传递给一个数组,然后由随机图像生成器调用。我错过了一些东西(或许多东西)。有什么想法吗?
NSString *chords = [[NSBundle mainBundle] pathForResource:@"ChordCharts" ofType:nil];
chordCharts = [NSMutableArray arrayWithContentsOfFile:chords];
int randomimages = rand() % 4;
switch (randomimages) {
case 0:
chordImage.image = [UIImage imageNamed:[chordCharts objectAtIndex:0]];
break;
case 1:
chordImage.image = [UIImage imageNamed:[chordCharts objectAtIndex:1]];
break;
case 2:
chordImage.image = [UIImage imageNamed:[chordCharts objectAtIndex:2]];
break;
case 3:
chordImage.image = [UIImage imageNamed:[chordCharts objectAtIndex:3]];
break;
default:
break;
}
答案 0 :(得分:0)
嗯,问题是什么?如果您希望文件看起来像这样
Name1.gif
Name2.jpg
...
然后它不会工作; " initWithContentsOf"在数组上需要一个plist文件:
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0"><array>
<string>Name1.gif</string>
<string>Name2.jpg</string>
...
</array></plist>
如果你想要文件的第一个版本,你只需将文件读入一个字符串,然后使用componentsSeparatedByString:@&#34; \ n&#34;把它分成几行:
NSString* MyString=[NSString stringWithContentsOfFile:@"..."];
NSArray* chordCharts=[MyString componentsSeparatedByString:@"\n"];
您可能想要检查空行,并且可能还要修剪行(以删除开头和结尾的空格)