[英]Get NSData from a file
I am trying get NSData of an own file. 我正在尝试获取自己文件的NSData。
My code is as follow, but NSData returned is always nil… (As you can see, I check if the file exists previously) 我的代码如下,但是返回的NSData始终为nil…(如您所见,我检查文件是否先前存在)
if ([[NSFileManager defaultManager] fileExistsAtPath:path]){
NSData * data = [[NSFileManager defaultManager] contentsAtPath:path];
}
Any idea? 任何想法? Thanks!
谢谢!
It's possible that path is a folder, in which case fileExistsAtPath will return YES, but no data can be read. 路径可能是一个文件夹,在这种情况下,fileExistsAtPath将返回YES,但无法读取任何数据。 You can add some extra debugging by reading the data as follows:
您可以通过如下读取数据来添加一些额外的调试:
NSError* error = nil;
NSData* data = [NSData dataWithContentsOfFile:path options:0 error:&error];
NSLog(@"Data read from %@ with error: %@", path, error);
The log output will display the actual error that occurred. 日志输出将显示发生的实际错误。
Use This code it works 使用此代码有效
NSString *path = [pathURL filePath];
if([[NSFileManager defaultManager] fileExistsAtPath:path)
{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
}
else
{
NSLog(@"File not exits");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.