我有以下方法,用于获取文本文件中包含的所有单词并将它们添加到数组中。我认为问题出在我的.txt文件所在的位置。我将它添加到类旁边的项目资源管理器中。我在其他地方已经读到它应该进入你的资源文件夹,但我认为较新版本的XCode不再为你创建资源文件夹。无论这是我的方法:
-(void) getWords
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"WordList" ofType:@"txt"];
NSString *wordFile = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray* words = [wordFile componentsSeparatedByString:@"\n"];
NSLog(@"Found %lu words", (unsigned long)[words count]);
}
有人可以帮我找到这个方法找到我的文件吗?我在哪里可以放置.txt文件?感谢。