【问题标题】:Why an NSException error?为什么会出现 NSException 错误?
【发布时间】:2011-09-08 19:49:50
【问题描述】:

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

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?

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

谢谢,

詹姆斯

【问题讨论】:

    标签: iphone objective-c ios xcode nsexception


    【解决方案1】:

    您正在尝试在字典初始化中格式化字符串,它期望格式为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];
    

    【讨论】:

      【解决方案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];
      

      【讨论】:

      • 这样做了,它的工作原理,谢谢!但是当它发送电子邮件时(因为它就是这样做的),它只是说......这是:然后什么都没有,当它应该添加来自 UITextField 的文本时......这是我使用的代码......@987654321 @我在这里声明了“messagebody”:pastie.org/2504667.
      • 在初始化 *plainpart NSDictionary 之前检查是否声明了 messagebody。或者,如果 NSMutableDictionary 在初始化后发生变异(更改),则需要使用它。
      【解决方案3】:

      您缺少一个论点。我计算了 8 个总论点,包括 nil。这意味着您的一个键值对不完整。

      【讨论】:

        【解决方案4】:

        尝试首先创建字符串:

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

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

        【讨论】:

        • 对不起,如果这听起来很傻,但我将如何“将此字符串作为对象放入字典”? :-)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-22
        • 2011-04-23
        • 2021-08-04
        相关资源
        最近更新 更多