无法访问资源文件

时间:2013-02-14 16:37:10

标签: ios objective-c embedded-resource

我正在开发一个iOS应用程序,我想将两个CSV文件加载到我的应用程序中。

如下图所示,这些文件位于 Resources 组中:

enter image description here

我用它来获取文件路径:

filePath = [[NSBundle mainBundle] pathForResource:@"Familias" ofType:@"txt"];

这是Util readTitleFromCSV:

的代码
+(void)readTitleFromCSV:(NSString*)path Entity:(NSString*)entityName
{
    AppDelegate* appDelegate =
        (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSString *fileDataString =
        [NSString stringWithContentsOfFile:path
                                  encoding:NSUTF8StringEncoding
                                     error:nil];

    NSArray *linesArray = [fileDataString componentsSeparatedByString:@"\n"];

    [ ... ]

}

fileDataString为零,我没有收到任何错误。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

NSError传递到您的文件中,并检查通话失败的原因。可能有一些编码问题。

在此之前,请转到目标构建阶段窗格,检查该文件是否包含在您的应用包中(请参阅下面的图片获取指导)

enter image description here