ios - Objective-C NSData 返回 nil
<p><p>我这里有这个方法,它可以创建 PDF 和 PDF 的 URL。我的问题是我正在尝试返回 PDF 的 NSData,但是当我尝试在方法结束时它返回 nil。为什么它返回 nil ?我该如何解决这个问题?</p>
<pre><code>- (NSData *) setProductionSchedulePDF
{
NSString *documentsDirectory = ;
NSString *filename = @"test.pdf";
NSURL *fileURL = ];
CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, NULL, NULL);
CGPDFContextBeginPage(pdfContext, NULL);
UIGraphicsPushContext(pdfContext);
CGRect bounds = CGContextGetClipBoundingBox(pdfContext);
CGContextScaleCTM(pdfContext, 1.0, -1.0);
CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height);
NSArray *taskArray = self.taskData;
UIFont *headerFont = ;
UIFont *font = ;
NSDictionary *attributes = @{ NSFontAttributeName: font};
NSDictionary *headerAttributes = @{ NSFontAttributeName: headerFont};
[@"Task" drawAtPoint:CGPointMake(10, 10) withAttributes:headerAttributes];
int counter = 0;
for (id object in taskArray) {
;
counter++;
}
int keyCounter = 0;
for(id object in self.prodSchedSortedKeys) {
;
NSArray *bfsmArray;
if(!=0)
{
bfsmArray = ;
}
else
{
bfsmArray = nil;
}
bfsmArray = nil;
int valueCounter = 0;
for(id object2 in bfsmArray)
{
if(bfsmArray != nil)
{
if(valueCounter == 13 || valueCounter == 66 || valueCounter == 67){
[ drawAtPoint:CGPointMake(60 * keyCounter + 90, 10.5 * valueCounter + 30) withAttributes:attributes];
}
else if (valueCounter == 68 || valueCounter == 69)
{
[[ baseLineStart] drawAtPoint:CGPointMake(60 * keyCounter + 90, 10.5 * valueCounter + 30) withAttributes:attributes];
}
else
{
if([[ baseLineStart] isEqualToString:@""] && [[ actualFinish] isEqualToString:@""]){
}
else
{
[ baseLineStart],[ actualFinish]] drawAtPoint:CGPointMake(60 * keyCounter + 90, 10.5 * valueCounter + 30) withAttributes:attributes];
}
}
}
NSLog(@"%@", object2);
valueCounter++;
}
keyCounter++;
}
UIGraphicsPopContext();
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
NSString *myString = ;
NSData *pdfData = ;
return pdfData;
}
</code></pre>
<p><strong>更新</strong></p>
<p>我像这样添加了 NSError:</p>
<pre><code>NSError *error;
NSData *pdfData = ;
NSLog(@"%@", error);
</code></pre>
<p>并且 NSError 返回以下内容:</p>
<pre><code>error NSError * domain: @"NSCocoaErrorDomain" - code: 260 0x16820310
</code></pre>
<p>这是错误的控制台日志:</p>
<pre><code>Error Domain=NSCocoaErrorDomain Code=260 "The file “test.pdf” couldn’t be opened because there is no such file." UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/A2C316BA-97DF-4560-8F6D-BA6A15B2CCA3/Documents/test.pdf, NSUnderlyingError=0x155420e0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>变化:</p>
<pre><code>NSString *myString = ;
</code></pre>
<p>收件人:</p>
<pre><code>NSString *myString = ;
</code></pre>
<p>原因是 <code></code> 需要一个路径,即 <code>/some/dir/file.pdf</code>。 <code>absoluteString</code> 返回一个 URL,即:<code>file://some/dir/file.pdf</code>。</p>
<p>您可以在 <a href="https://stackoverflow.com/questions/16176911/nsurl-path-vs-absolutestring" rel="noreferrer noopener nofollow">question</a> 中阅读更多关于 <code>path</code> 和 <code>absoluteString</code> 之间区别的信息。 .</p></p>
<p style="font-size: 20px;">关于ios - Objective-C NSData 返回 nil,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33129071/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33129071/
</a>
</p>
页:
[1]