操作无法完成.(可可错误260)



我是IOS开发的新手,我正在开发pdf应用程序,我需要在NSData变量上存储pdf文件,我有pdf路径,但当我尝试使用dataWithContentsOfFile将此pdf放在NSData变量上时,我得到此消息错误她是我的简单代码:

NSError *error;
NSString *PdfPath = [NSString stringWithFormat:(@"%@"),document.fileURL ];
NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:@"file://localhost" withString:@""];
NSLog(@"OriginalPdfPath => %@", newPath);
NSData *pdfData = [NSData dataWithContentsOfFile:newPath options:NSDataReadingUncached error:&error];

注意:pdf路径的格式为:/Users/bluesettle/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/BBEF320E-7E2A-49DA-9FCF-9CFB01CC0402/ContractApp.app/Pro.iOS.Table.Views.pdf

谢谢你的帮助

Cocoa错误260是NSFileReadNoSuchFileError(如FoundationErrors.h所列),这意味着在您指定的路径中无法找到该文件。

问题是你的路径仍然包含编码的空格(%20),因为你是基于URL。您可以简单地这样做:

NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path]];

尝试使用NSBundle

NSString *newPath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"pdf"]

编辑:

那么你可以使用bundleWithPath方法,这里有一个例子:

NSString *documentsDir= [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];

最新更新