【问题标题】:objective c inserting nil object from dictionary?目标c从字典中插入零对象?
【发布时间】:2016-01-24 17:23:38
【问题描述】:

我正在尝试将HTML 转换为attributedText 并将其存储在UITextView 中。我不断收到错误

[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: 尝试从 objects[0] 插入 nil 对象

我检查了我正在使用的字典的值,它们不是 nil,我看不出问题出在哪里。

属性文本已生成,并且按预期具有红色文本中的值“Your Message Here”,但是当我尝试将其插入此行 tx.attributedText = attrString; 时,我收到了该错误。

  UITextView *tx =[[UITextView alloc]init];
  [tx setReturnKeyType:UIReturnKeyDone];
  [tx setTag:1];
  tx.delegate = self;

   NSString *htmlString = @"<bold>test html</bold>";


  NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

  NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
      options:options
      documentAttributes:nil
      error:nil];

  tx.attributedText = attrString;
  return tx;

编辑:我已经简化了代码,我已经删除了 web 请求,只插入了纯 html。无论在那里插入什么,我仍然得到同样的错误

这是htmlString 的原始值:我可能在粘贴值时搞砸了

"&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;&lt;title&gt;&lt;/title&gt;&lt;meta name="Generator" content="Cocoa HTML Writer"&gt;&lt;style type="text/css"&gt;p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px ".SF UI Text"; color: #ff0000; background-color: #ffffff}span.s1 {font-family: ".SFUIText-Regular"; font-weight: normal; font-style: normal; font-size: 14.00pt; text-decoration: line-through}&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="p1"&gt;&lt;span class="s1"&gt;Your Message Here!&lt;/span&gt;&lt;/p&lt;/body&gt;&lt;/html&gt;""

options

[0] (null)  @"DocumentType" : @"NSHTML" 

attrString

`attrString NSConcreteAttributedString *    @"hi test html"

attributes NSRLEArray * 0x7fa0fd8053a0 0x00007fa0fd8053a0

里面是NSObjectisa Class NSRLEArray 0x000000010a03ede8

【问题讨论】:

  • 您正在尝试将 nil 插入字典;不是一个人。
  • 通过提供的信息真的很难猜出你的问题,你能提供异常发生时的堆栈吗?
  • 提供实际的原始 JSON 也可能会有所帮助。
  • 请给我们应用程序停止执行的行。您的代码中有一个字典文字,但它不太可能是崩溃的那个。
  • 请注意,我们通常不会分配/初始化 NSError 参数。没必要。

标签: objective-c uitextview nsdictionary nsattributedstring


【解决方案1】:

出于某种原因,如果上面的代码它没有在主线程上运行,我必须将 textView 传递给函数并将 html 转换为其中的属性文本

    - (UITextView *)requestMessageReply:(UITextView*)textView {

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  [request setHTTPMethod:@"GET"];
  [request setURL:[NSURL URLWithString:@"http://lurl"]];

  NSError *error = [[NSError alloc] init];
  NSHTTPURLResponse *responseCode = nil;

  NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

  if([responseCode statusCode] != 200){
    NSLog(@"Error getting HTTP status code %li", (long)[responseCode statusCode]);
     }

  NSString * responseString = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];

  NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];

  NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:nil];


  //  NSString *htmlString = [self requestMessageReply];
  NSString *htmlString = (NSString *)JSON[@"message"];
  //(NSString *)JSON[@"message"]
  NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};

  NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding]
                                                                    options: options
                                                         documentAttributes:nil error:nil];
  textView.attributedText = attrString;
  return textView;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多