Ios stringWithContentsOfFile期间记录错误消息

Ios stringWithContentsOfFile期间记录错误消息,ios,objective-c,Ios,Objective C,我正在尝试读取CSV文件。我已经在应用程序中导入了该文件,因此该文件是本地文件。当我执行下面这部分代码时: @autoreleasepool { NSError *error; NSString *alldata = [NSString stringWithContentsOfFile:@"config_menu_position.csv" encoding:NSUTF16St

我正在尝试读取CSV文件。我已经在应用程序中导入了该文件,因此该文件是本地文件。当我执行下面这部分代码时:

@autoreleasepool {
    NSError *error;
    NSString *alldata =  [NSString stringWithContentsOfFile:@"config_menu_position.csv"
                                                   encoding:NSUTF16StringEncoding error:&error];
    NSLog(@"alldata: %@", alldata);
    NSLog( @"error: %@", error );
}
我有此日志错误消息,并且我的变量始终为nul:

    2013-04-21 17:05:37.571 PageViewController[1578:c07] alldata :(null)
    2013-04-21 17:05:38.242 PageViewController[1578:c07] error: Error Domain=NSCocoaErrorDomain
 Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x7ebf100 
{NSFilePath=config_menu_position.csv, NSUnderlyingError=0x7ebf060 "The operation couldn’t be 
completed. No such file or directory"}

有什么想法吗?

如果缺少文件的完整路径,请尝试以下操作:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"config_menu_position" ofType:@"csv"];
NSString *alldata = [NSString stringWithContentsOfFile:filePath
                                              encoding:NSUTF16StringEncoding 
                                                 error:&error];

请从
NSBundle
中尝试
pathForResource:of type
,例如

[[NSBundle mainBundle] pathForResource:@"config_menu_position" ofType:@"csv"]
然后使用A
NSString
初始值设定项中返回的路径。

太好了!!!我已将“NSUTF16StringEncoding”更改为“NSUTF8StringEncoding”,因为我有汉字:)。谢谢!