stringWithContentsOfFile:编码:错误:错误 260

发布于 2024-11-13 09:36:44 字数 1677 浏览 0 评论 0原文

我有一个小型实用程序应用程序,它解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-11-20 09:36:44

它失败是因为您没有传递文件的实际位置,而只是传递文件名,因此它尝试在程序自己的文件夹中查找它 - 您正在使用捆绑包的路径。除非您的 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.

机场等船 2024-11-20 09:36:44

简而言之,您传递的文件路径不正确,因此找不到或不存在。

To put it simply the file path you passed is incorrect thus can't be found or does not exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文