简体   繁体   English

在iPhone应用程序中读取文件时出现问题!

[英]problem in reading files in iphone application!

I am reading a file in iphone app. 我正在iPhone应用程序中读取文件。 Its Works Fine. 它的作品很好。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"%@",documentsDirectory);
    NSString *fileName = [NSString stringWithFormat:@"%@/test.txt",documentsDirectory];
    NSLog(@"%@",fileName);
    NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
                                                    usedEncoding:nil
                                                           error:nil];
    NSLog (@"%@",content);
}

works good when i am using filename as test.txt 当我使用文件名作为test.txt时效果很好

But when i add another file in the resource suppose test1.txt then NSLog(@"%@",documentsDirectory) and NSLog(@"%@",fileName) shows the right result. 但是,当我在资源中添加另一个文件时,假设test1.txtNSLog(@"%@",documentsDirectory)NSLog(@"%@",fileName)显示正确的结果。 But

NSLog (@"%@",content); prints null in the log . log输出null So what is the reason? 那是什么原因呢?

I am printing detail error and it prints 我正在打印详细信息错误并打印

NSFilePath = "/Users/sam-xxx/Library/Application Support/iPhone Simulator/3.2/Applications/51197946-6042-4A90-AA39-F07F8A649308/Documents/test1.txt";
    NSUnderlyingError = Error Domain=NSPOSIXErrorDomain Code=2 "Operation could not be completed. No such file or directory";

It would be best here to check to see if an error is returned: 最好在这里检查是否返回错误:

NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
                                                usedEncoding:nil
                                                       error: &error];
if (error) NSLog(@"Error !: %@", [error localizedDescription]);

That will (hopefully) give you a clue as to whats going on. 这样(希望)会为您提供提示。

(Edited to give example bundle resource usage as the file is in the bundle not the Documents directory). (已编辑,以提供示例捆绑软件资源使用情况,因为该文件位于捆绑软件中,而不是在Documents目录中)。

Docs for NSBundle are here: NSBundle Documentation NSBundle的文档在这里: NSBundle文档

You have 2 choices, the one you suggest: 您有2个选择,您建议一个:

NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

Which will return the Bundle resource directory with the filename appended to that path. 它将返回Bundle资源目录,并将文件名附加到该路径。

Personally I prefer the pathForResource:ofType: method: 我个人更喜欢pathForResource:ofType:方法:

NSString *filename = [[NSBundle mainBundle] pathForResource: @"test1" ofType: @"txt"];

As this will not only tell you if the file exists (returns nil if it does not) but will also search the localisation directories if you have them. 因为这不仅会告诉您该文件是否存在(如果不存在,则返回nil),如果有,还将搜索本地化目录。

Did you check if the file in the error message exists? 您是否检查错误消息中的文件是否存在? When run in the simulator you can simply open Finder, press cmd+shift+g and paste the directory to the file. 在模拟器中运行时,您只需打开Finder,按cmd + shift + g并将目录粘贴到文件中即可。

Maybe you didn't add it to the target? 也许您没有将其添加到目标中? Open the information window for the file in your resources and check which targets are checked. 打开资源中文件的信息窗口,然后检查选中了哪些目标。

When you run a app in Simulator, you could copy and paste the right file into the app's Documents directory use Finder, but on iPhone or iTouch how could you paste a file into the Documents directory. 在Simulator中运行应用程序时,可以使用Finder将正确的文件复制并粘贴到应用程序的Documents目录中,但是在iPhone或iTouch上,如何将文件粘贴到Documents目录中。

We got only 2 method to put something into Documents directory when app is running on iPhone or iTouch. 当应用程序在iPhone或iTouch上运行时,只有2种方法可以将某些内容放入Documents目录。

  1. Create one. 创建一个。
  2. Copy one from the bundle. 从捆绑中复制一个。

in your code, you read a file in the Documents directory, you could be paste a file in the directory manually use Finder. 在代码中,您读取了Documents目录中的文件,可以使用Finder手动将文件粘贴到目录中。 Try this. 尝试这个。

NSString *path = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
NSString *content = [[NSString alloc] initWithContentsOfFile:path
                                                usedEncoding:nil
                                                       error:nil];
NSLog (@"%@",content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM