操作无法完成。 (可可误差260)(The operation couldn’t be comp

2019-09-02 01:14发布

我对IOS开发新我工作的PDF应用程序,我需要存储在一个NSData的变量PDF文件,我有PDF路径,但我得到这个消息错误,当我试图把这个PDF上的NSData使用dataWithContentsOfFile她是我的简单的代码变量:

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

谢谢你的帮助

Answer 1:

可可错误260是NSFileReadNoSuchFileError (如在上市FoundationErrors.h ),这意味着文件无法在您指定的路径中。

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

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


Answer 2:

尝试使用NSBundle

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

编辑:

比你可以使用bundleWithPath方法,下面是一个例子:

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

NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];


文章来源: The operation couldn’t be completed. (Cocoa error 260.)