Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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 完成处理程序块内的NSSavePanel nil_Objective C_Cocoa_Osx Lion_Nssavepanel - Fatal编程技术网

Objective c 完成处理程序块内的NSSavePanel nil

Objective c 完成处理程序块内的NSSavePanel nil,objective-c,cocoa,osx-lion,nssavepanel,Objective C,Cocoa,Osx Lion,Nssavepanel,我正在尝试将视图的内容保存到PDF文件中。您看到的代码位于从NSView继承的类中: - (IBAction) savePDF: (id) sender { __block NSSavePanel* panel=[NSSavePanel savePanel]; [panel setAllowedFileTypes: [NSArray arrayWithObject: @"pdf"]]; [panel beginSheetModalForWindow: [self wind

我正在尝试将视图的内容保存到PDF文件中。您看到的代码位于从NSView继承的类中:

- (IBAction) savePDF: (id) sender
{
    __block NSSavePanel* panel=[NSSavePanel savePanel];
    [panel setAllowedFileTypes: [NSArray arrayWithObject: @"pdf"]];
    [panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result)
     {
         if(result==NSOKButton)
         {
             NSCAssert(panel!=nil,@"panel is nil");
             NSData* data=[self dataWithPDFInsideRect: [self bounds]];
             NSError* error;
             BOOL successful=[data writeToURL: [panel URL] options: 0 error: &error];
             if(!successful)
             {
                 NSAlert* alert=[NSAlert alertWithError: error];
                 [alert runModal];
             }
         }
     }];
    panel=nil;
}
该方法通过菜单触发。
问题在于断言失败:

NSCAssert(panel!=nil,@"panel is nil");

即使我将NSSavePanel指针声明为_块。为什么?

实际上,答案是删除
\u_块
说明符

我用/不用它运行了你的代码。Assert在没有它的情况下工作(即
\u块
说明符)失败

现在,回答为什么:

我假设
\u块
说明符是为全局/实例变量而不是局部变量创建的,但我可能错了


X'D。。。我不知道是什么击中了我,但请检查:

NSSavePanel* panel=[NSSavePanel savePanel];

[panel setAllowedFileTypes: [NSArray arrayWithObject: @"pdf"]];
[panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result)
 {
     if(result==NSOKButton)
     {
         dispatch_async(dispatch_get_main_queue(), ^{
             NSCAssert(panel!=nil,@"panel is nil");
             NSData* data=[self dataWithPDFInsideRect:[self bounds]];
             NSError* error;
             BOOL successful=[data writeToURL: [panel URL] options: 0 error: &error];
             if(!successful)
             {
                 NSAlert* alert=[NSAlert alertWithError: error];
                 [alert runModal];
             }
         });
     }
 }];
panel=nil;
我刚刚决定,让我把它包在一个GCD块中,然后在主线程上绘制它。您的代码工作得非常出色。该视图按预期绘制到PDF,我可以确认:D

至于这个问题,似乎很明显。在后台线程和绘图中调用边界是不允许的


这很有趣,谢谢:D

解决方案:由于未知的原因,这对我来说仍然是一个疑问,如果我对视图类进行iAction,[self-bounds]总是重新运行nszerorrect。在其他方法中,它返回正确的值。因此我解决了这个问题,从NSSavePanel中删除u块说明符并重写(也重新绑定)app delegate类中的方法。

是的,我使用ARC。我尝试删除_块说明符。现在断言没有失败,文件被保存。但是当我使用预览打开文件时,有一个空PDF文件。这也是因为[self bounds]没有返回正确的值,如果我尝试打印它,它会显示[self bounds].size.width在块内为零。如何让这些变量在块内继续存在?是的,我运行了你的代码,边界也返回了CGRectZero。。我去看看。我也很想知道。