English 中文(简体)
阅读当地文本档案的问题[SString stringWithContentsOfFile...],目标回归者无效
原标题:Problems reading local text file with [NSString stringWithContentsOfFile...] with Objective-c returns null

这似乎并不复杂。 我只想制造一个带有当地文本档案内容的直观物体,称为“测试.txt”,我已将这一文件放在我的项目根基。

我正试图装上这部法律。

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"

我不肯定,为什么这样做是失败的,而不是我的文本文件的内容。 档案编码似乎合适。 此外,档案也存在。 我把档案拖到错误地点? 我是否不得不以某种方式在我的NSBundle主食上添加这一内容?

感谢!

问题回答

如果您实际填写了<代码>error para amount in +stringWithContentsOfFile:encoding:error: 之后,将tell You,为何将其退回nil。 你确实应该这样做。 在这条道路上,不可能有档案(或没有许可阅读)。

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.

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.

这里的所有答复都稍有帮助。 尤其是关于填补这一错误的建议:不回到零。

最后,我发现,我不得不人工复制I号卷宗,试图向与汇编的双版目录开放。 仅将其复制到Xcode项目似乎对我不可行。

我与你文森特一样面临同样的问题。 我的理解是,由于我正在提出以青少年为基础的申请,因此,它没有以与“SOS”同样的方式汇编和将档案列入一个空白处。 因此,它没有按照预期列入档案。 我得以开展工作,在我的硬车上打下档案的完整目录。

Drag, 并拒绝接受你的测试。 建设阶段和阶段的轴心文件;





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签