目标C:阅读文本文件 [英] Objective C: Reading text files
问题描述
我以前已经做过,但是现在对我不起作用.我在做:
I've done this before, but its not working for me now. I'm doing:
NSString* path = [[NSBundle mainBundle] pathForResource:@"test"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSLog(@"%@",path);
,并且每次我执行NSLog路径和内容时,它都会返回(null)
.有人可以看到我在做什么错吗?
and it returns (null)
every time when I NSLog path and content. Can anyone see what I'm doing wrong?
推荐答案
如果您将其传递为无法打开的路径,则内容将为nil(记录为(null)").因此,您唯一的问题是,NSBundle的相关实例无法在应用程序包的资源部分中找到test.txt.
content will be nil (which logs as '(null)') if you pass it a path it can't open. So your only issue is that the relevant instance of NSBundle is unable to find test.txt within the resources part of your application bundle.
您应该:
- 检查文件是否在您的Xcode项目中;并且,如果是,
- 检查它是否包含在所选目标(位于正常Xcode窗口布局左侧的项目树视图中)下方的复制捆绑资源"阶段中,如果是,则
- 在生成的应用程序包中查找(找到您的产品,右键单击,选择在Finder中显示",在Finder中右键单击该应用程序并选择显示包内容",然后在其中查找您的文件)以确保它在那里.
如果将其复制到其中,但是相关的NSBundle实例找不到,那么即将发生一些非常奇怪的事情.
If it's copied in but the relevant instance of NSBundle can't find it then something very strange is afoot.
这篇关于目标C:阅读文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!