xcode Problems reading local text file with [NSString stringWithContentsOfFile...] with Objective-c returns null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8876217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems reading local text file with [NSString stringWithContentsOfFile...] with Objective-c returns null
提问by Vincent
This does not seem so complex. I just want to create a string object with the contents of a local text file called "test.txt" which I have placed in the root of my project.
This does not seem so complex. I just want to create a string object with the contents of a local text file called "test.txt" which I have placed in the root of my project.
I am trying to load it w/ the following code.
I am trying to load it w/ the following code.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
NSString *textData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//The below works, but not my above... not sure why.
//textData = @"TEST TEST";
NSLog(@"Some text from a file of %@", textData);
NSLog(@"The length of the string is %lu", [textData length]);
The output from this code is "Some text from a file of (null)" "The length of the string is 0"
The output from this code is "Some text from a file of (null)" "The length of the string is 0"
I am not sure why this is returning null instead of the contents of my text file. The encoding for the file seems appropriate. Also, the file exists. Am I dragging the file to the wrong location? Do I have to add it to my NSBundle mainBundle in some way?
I am not sure why this is returning null instead of the contents of my text file. The encoding for the file seems appropriate. Also, the file exists. Am I dragging the file to the wrong location? Do I have to add it to my NSBundle mainBundle in some way?
Thanks!
Thanks!
回答by Lily Ballard
If you actually fill in the error
parameter in +stringWithContentsOfFile:encoding:error:
then it would tell youwhy it's returning nil
. You really ought to do that. The likely reason is there is no file at that path (or you don't have permissions to read it).
If you actually fill in the error
parameter in +stringWithContentsOfFile:encoding:error:
then it would tell youwhy it's returning nil
. You really ought to do that. The likely reason is there is no file at that path (or you don't have permissions to read it).
回答by YuAo
NSLog the filePath and see if it's nil. If the filePath is nil, I think you didn't put the file in right place. You should drag the file into the project navigator of your project in Xcode. Also, double check the file name.
NSLog the filePath and see if it's nil. If the filePath is nil, I think you didn't put the file in right place. You should drag the file into the project navigator of your project in Xcode. Also, double check the file name.
回答by X Slash
Log your filePath to see if it can actually find the file, if not, you probably add your test.txt in a wrong way. To add to main bundle, drag your test.txt into your xcode project, when prompted, just tick Copy items into destination group's folder (if needed) and select Create groups for any added folders.
Log your filePath to see if it can actually find the file, if not, you probably add your test.txt in a wrong way. To add to main bundle, drag your test.txt into your xcode project, when prompted, just tick Copy items into destination group's folder (if needed) and select Create groups for any added folders.
回答by Vincent
All of the answers here helped a little bit. In particular the suggestion to fill in the error: in a way that does not return nil.
All of the answers here helped a little bit. In particular the suggestion to fill in the error: in a way that does not return nil.
Eventually I found that I had to manually copy the .txt file I was trying to open to the same directory as the binary that was compiled. Just copying it into the xcode project did not seem to work for me.
Eventually I found that I had to manually copy the .txt file I was trying to open to the same directory as the binary that was compiled. Just copying it into the xcode project did not seem to work for me.
回答by Joshua
I was having the exact same problem as you, Vincent. My understanding is that because I am making an console-based application, it doesn't compile and include files within a bundle in the same way that an iOS app would. Therefore, it's not including the file as expected. I was able to get it to work by typing out the full directory location of the file on my hard drive.
I was having the exact same problem as you, Vincent. My understanding is that because I am making an console-based application, it doesn't compile and include files within a bundle in the same way that an iOS app would. Therefore, it's not including the file as expected. I was able to get it to work by typing out the full directory location of the file on my hard drive.
回答by krishna
Drag and drop your test.txt file to Build Phases->copy bundle resources
Drag and drop your test.txt file to Build Phases->copy bundle resources