目标 C:读取文本文件
我以前这样做过,但现在对我不起作用。我正在做:
NSString* path = [[NSBundle mainBundle] pathForResource:@"test"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSLog(@"%@",path);
每次当我 NSLog 路径和内容时它都会返回 (null)
。谁能看到我做错了什么吗?
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);
and it returns (null)
every time when I NSLog path and content. Can anyone see what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你向它传递一个它无法打开的路径,则内容将为 nil(记录为“(null)”)。因此,您唯一的问题是 NSBundle 的相关实例无法在应用程序包的资源部分中找到 test.txt。
您应该:
如果它被复制了,但 NSBundle 的相关实例找不到它,那么就会出现非常奇怪的情况。
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.
You should:
If it's copied in but the relevant instance of NSBundle can't find it then something very strange is afoot.