stringWithContentsOfFile:编码:错误:错误 260
我有一个小型实用程序应用程序,它解析 csv 文件并使用数据填充核心数据存储以与另一个应用程序一起使用。我使用 -initWithContentsOfFile:encoding:error:
打开 csv 文件,该方法总是返回 nil 并出现错误。
Error Domain=NSCocoaErrorDomain Code=260 UserInfo=0x100106450 "The file “data.csv” couldn’t be
opened because there is no such file." Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 "The
operation couldn’t be completed. No such file or directory"), {
NSFilePath = "/Users/****/Library/Developer/Xcode/DerivedData/CreateData-
bhkmspyczgcfcrgehcbaydwdbfoa/Build/Products/Debug/data.csv";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be
completed. No such file or directory\"";
}
现在,我正在查看该确切目录中的确切文件。更令人困惑的是,我有另一个版本的基本上相同的实用程序,可用于另一个可以运行的应用程序 - 唯一的区别是核心数据存储和实体的构成以及 csv 文件中的列数,所有这些都是加载 csv 文件后调用。或者无法加载,就像......
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *csvFile = [[NSString alloc] initWithContentsOfFile:csvFilePath(QUESTIONS_INPUT_FILENAME) encoding:NSASCIIStringEncoding error:&error]; // This fails
if (error != nil) {
NSLog(@"Returned error:\n%@, %@", error, [error userInfo]); // Returns the above error message
exit(1);
}
// ...
}
NSString *csvFilePath(NSString *inputFileName) {
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:inputFileName];
}
NSString *applicationDocumentsDirectory() {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
I've got a small utility application that parses a csv-file and uses the data to fill a Core Data store for use with another application. I open the csv-file using -initWithContentsOfFile:encoding:error:
and the method always returns nil with the error
Error Domain=NSCocoaErrorDomain Code=260 UserInfo=0x100106450 "The file “data.csv” couldn’t be
opened because there is no such file." Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 "The
operation couldn’t be completed. No such file or directory"), {
NSFilePath = "/Users/****/Library/Developer/Xcode/DerivedData/CreateData-
bhkmspyczgcfcrgehcbaydwdbfoa/Build/Products/Debug/data.csv";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be
completed. No such file or directory\"";
}
Now, I'm looking at that exact file in that exact directory. Even more confusing is the fact that I have another version of essentially the same utility for another app that works -- the only difference is the makeup of the core data store and entities and the number of columns in the csv file, all of which are invoked after loading the csv file. Or failing to load, as it were ...
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *csvFile = [[NSString alloc] initWithContentsOfFile:csvFilePath(QUESTIONS_INPUT_FILENAME) encoding:NSASCIIStringEncoding error:&error]; // This fails
if (error != nil) {
NSLog(@"Returned error:\n%@, %@", error, [error userInfo]); // Returns the above error message
exit(1);
}
// ...
}
NSString *csvFilePath(NSString *inputFileName) {
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:inputFileName];
}
NSString *applicationDocumentsDirectory() {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它失败是因为您没有传递文件的实际位置,而只是传递文件名,因此它尝试在程序自己的文件夹中查找它 - 您正在使用捆绑包的路径。除非您的 csv 文件所在的位置,否则它不会找到它。
It fails because you aren't passing in the actual location of the file, just the filename, so it is trying to look for it in the program's own folder - you're using the path of the bundle. unless that's where your csv file is, it won't find it.
简而言之,您传递的文件路径不正确,因此找不到或不存在。
To put it simply the file path you passed is incorrect thus can't be found or does not exist.