本地文件中的 nsstring 出现问题

发布于 2024-12-25 12:30:35 字数 576 浏览 4 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欲拥i 2025-01-01 12:30:36

我认为这解释了您需要什么:

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).

仙气飘飘 2025-01-01 12:30:36

我找到了一种在本地获取它的方法。我将代码更改为:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"highscore1" ofType:@"txt"];
    NSString *highscore1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

无论如何,谢谢您的帮助。

I found a way to get it locally. I changed my code to this:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"highscore1" ofType:@"txt"];
    NSString *highscore1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

Thanks for the help anyway.

浪漫人生路 2025-01-01 12:30:36

当您将该文件添加到 XCode 中的项目时,您是否选择了将项目复制到目标组的文件夹中

When you added that file to your project in XCode, did you select Copy items into destination group's folder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文