ios / iphone - [NSNull dataUsingEncoding:]:发送到实例的无法识别的选择器

时间:2014-03-21 08:44:10

标签: ios iphone plugins

有人可以帮我解释一下所说的错误是什么吗?

[NSNull dataUsingEncoding:]: unrecognized selector sent to instance

我被困在iOS应用程序的打印机插件中。

我一直在努力让这个应用程序工作一段时间......被监视的代码可能有助于找到错误......

import“APPPrinter.h”

@interface APPPrinter (Private)

- (UIPrintInteractionController*) getPrintController;

- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller;

- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller;

- (void) informAboutResult:(int)code callbackId:(NSString*)callbackId;

- (BOOL) isPrintServiceAvailable;

@end


@implementation APPPrinter

// Is printing available.

- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                   messageAsBool:[self isPrintServiceAvailable]];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}

- (void) print:(CDVInvokedUrlCommand*)command
{
if (![self isPrintServiceAvailable])
{
    return;
}

NSArray*  arguments  = [command arguments];
NSString* content    = [arguments objectAtIndex:0];

UIPrintInteractionController* controller = [self getPrintController];

[self adjustSettingsForPrintController:controller];
[self loadContent:content intoPrintController:controller];

[self openPrintController:controller];

[self commandDelegate];

}

- (UIPrintInteractionController*) getPrintController
{
return [UIPrintInteractionController sharedPrintController];

}

- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller
{
UIPrintInfo* printInfo    = [UIPrintInfo printInfo];
printInfo.outputType      = UIPrintInfoOutputGeneral;
controller.printInfo      = printInfo;
controller.showsPageRange = YES;

return controller;

}

- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
{
// Set the base URL to be the www directory.
NSURL *basEURL = [[NSBundle mainBundle] URLForResource:@"Login" withExtension:@"html"];

// Load page into a webview and use its formatter to print the page
UIWebView* webPage    = [[UIWebView alloc] init];

[webPage loadHTMLString:content baseURL:basEURL];


UIViewPrintFormatter* formatter = [webPage viewPrintFormatter];

controller.printFormatter = formatter;
controller.showsPageRange = YES;
}
- (void) openPrintController:(UIPrintInteractionController*)controller
{
[self.commandDelegate runInBackground:^
{
[controller presentAnimated:YES completionHandler:NULL];

}];

[self performSelectorOnMainThread:@selector(openPrintController:) withObject:nil waitUntilDone:NO];
}

- (BOOL) isPrintServiceAvailable
{
Class printController = NSClassFromString(@"UIPrintInteractionController");

if (printController)
{
    UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];

    return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
}

return NO;
}

@end

创建和获取Print Plugin以在我的应用程序中运行的任何帮助/链接也都很棒。

1 个答案:

答案 0 :(得分:2)

这意味着消息“dataUsingEncoding:”被发送到NSNull的实例。

程序中的某个地方是一个显然认为它是NSString *的对象,但实际上它是一个NSNull *。 NSNull对象用作数组和字典中的占位符,表示“没有值”,因为您无法在数组和字典中存储nil。

在Xcode中,单击“Breakpoints”,单击“+”,然后添加“Exception breakpoint”。再次运行您的程序。这次它将在调试器中完全停止尝试调用dataUsingEncoding的代码:找出对象的来源并修复它。