在Xcode中获取文件的内容

mil*_*ilo 1 io xcode file objective-c

在我的项目中,我有一个名为GAME.js的纯文本文件,我正在尝试获取该文件的内容.

[NSString stringWithContentsOfFile:@"GAME.js" encoding:NSUTF8StringEncoding error:nil];
Run Code Online (Sandbox Code Playgroud)

听起来它应该工作,但它只是返回一个空字符串.

请帮我解决这个问题.

Chr*_*det 8

放入Xcode项目中GAME.jsResources文件夹,并使用以下代码获取文件的内容.

NSError *error = nil;
NSString* path = [[NSBundle mainBundle] pathForResource:@"GAME" ofType:@"js"];
NSString* gameFileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
Run Code Online (Sandbox Code Playgroud)