Search code examples
iphoneobjective-ciosxcodensexception

Why an NSException error?


I have put the following code in...;

NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
@"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %@",field.text,     
kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];

...and I receive an NSException error saying;

*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate:     
<NSInvalidArgumentException> +[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil.  Or, did  
you forget to nil-terminate your parameter list?

What does this mean? What do I have to do to fix this issue?

Thanks,

James


Solution

  • You are trying to format a string in your dictionary initialization and it expects the format to be object, key, object, key, etc... To fix try creating your formatted string on another line for clarity and then adding it as part of the objects and keys as so

    NSString *message = [NSString stringWithFormat:@"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %@",
                                                     field.text];
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"text/plain", kSKPSMTPPartContentTypeKey,
                                  message, kSKPSMTPPartMessageKey,
                                  @"8bit", kSKPSMTPPartContentTransferEncodingKey,nil];