Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c stringWithContentsOfFile:编码:错误:错误260_Objective C_Cocoa_Nsstring - Fatal编程技术网

Objective c stringWithContentsOfFile:编码:错误:错误260

Objective c stringWithContentsOfFile:编码:错误:错误260,objective-c,cocoa,nsstring,Objective C,Cocoa,Nsstring,我有一个小型实用程序应用程序,它解析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."

我有一个小型实用程序应用程序,它解析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];
}

它失败是因为您没有传递文件的实际位置,只是传递文件名,所以它试图在程序自己的文件夹中查找它-您使用的是包的路径。除非你的csv文件在那里,否则它不会找到它。

简单地说,你传递的文件路径不正确,因此找不到或不存在。

csvFilePath
是返回整个路径的方法。它恰好是应用程序包。除非他在他的应用程序包中有csv文件,否则它找不到它。“除非他在他的应用程序包中有csv文件”是一个重要的部分@royen,确保将CSV文件添加到项目中,并包含在目标的复制捆绑资源构建阶段。如果您的目标是命令行工具,而不是捆绑包中的应用程序,那么使用NSBundle就没有什么意义;相反,请设置您的Xcode方案,将文件名作为参数传递。好吧,这真是令人尴尬——事实证明错误发生在键盘和椅子之间。对不起,浪费了你的时间。。。