为什么NSException错误?

时间:2011-09-08 19:49:50

标签: iphone objective-c ios xcode nsexception

我已将以下代码放入......;

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];

...我收到NSException错误说

*** 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?

这是什么意思?我该怎么做才能解决这个问题?

谢谢,

詹姆斯

4 个答案:

答案 0 :(得分:4)

您正在尝试在字典初始化中格式化字符串,并且它希望格式为object, key, object, key, etc..。为了清楚起见,尝试在另一条线上创建格式化的字符串,然后将其作为对象和键的一部分添加为

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];

答案 1 :(得分:2)

也许你的意思是这样的:

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

答案 2 :(得分:1)

你错过了一个论点。我总计8个参数,包括零。这意味着您的一个键值对不完整。

答案 3 :(得分:1)

尝试第一次创建字符串:

NSString *partMessageKey = [NSString stringWithFormat:@"Hello,\n You've just received a new message from the iDHSB iPhone App.\n Here it is: %@",field.text];

然后将此字符串作为对象放入字典。