本地文件中的 nsstring 出现问题
我正在制作一个 iPhone 应用程序,我希望能够从文本文件加载高分。我创建了一个名为 highscores1.txt 的文件并将其添加到我的 xcode 项目中。当我尝试从文件中的文本创建 NSString 时,NSString 的值为 nil。这是我的代码:
NSString *highscore1 = [NSString stringWithContentsOfFile:@"highscore1.txt" encoding:NSUTF8StringEncoding error:NULL];
我尝试将文件路径更改为其完整路径,如下所示:
NSString *highscore1 = [NSString stringWithContentsOfFile:@"/Users/deepikama/Documents/games/Dodge Cars/Dodge Cars/highscore1.txt" encoding:NSUTF8StringEncoding error:NULL];
这会产生我想要找到的值。为什么完整路径有效而本地路径无效?我怎样才能使本地路径也正常工作?
I am making an iphone app and I want to be able to load high scores from text files. I made a file called highscores1.txt and added it to my xcode project. When I try to make an NSString from the text in the file, the NSString's value is nil. Here is my code:
NSString *highscore1 = [NSString stringWithContentsOfFile:@"highscore1.txt" encoding:NSUTF8StringEncoding error:NULL];
I tried changing the file path to its complete path like this:
NSString *highscore1 = [NSString stringWithContentsOfFile:@"/Users/deepikama/Documents/games/Dodge Cars/Dodge Cars/highscore1.txt" encoding:NSUTF8StringEncoding error:NULL];
And this results in the value that I was intending to find. Why does the complete path work but not the local path? How can I make the local path work as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这解释了您需要什么:
http://www.techotopia.com/ index.php/Working_with_Directories_on_iOS_4_(iPhone)#Identifying_the_Documents_Directory
另外,我打赌对你有用的东西只在模拟器上运行,而不是在实际设备上(目录结构不同)。
I think this explains what you need:
http://www.techotopia.com/index.php/Working_with_Directories_on_iOS_4_(iPhone)#Identifying_the_Documents_Directory
Also, I'm betting that what worked for you was only run on the simulator, and not on an actual device (where the directory structure is different).
我找到了一种在本地获取它的方法。我将代码更改为:
无论如何,谢谢您的帮助。
I found a way to get it locally. I changed my code to this:
Thanks for the help anyway.
当您将该文件添加到 XCode 中的项目时,您是否选择了将项目复制到目标组的文件夹中
When you added that file to your project in XCode, did you select Copy items into destination group's folder