objective c从字典中插入nil对象?

时间:2016-01-24 17:23:38

标签: objective-c uitextview nsdictionary nsattributedstring

我正在尝试将pdf(file = "Liposarcoma ARSA.pdf") p <- ggplot(data = samplev, aes(x = samples, y = Target)) p + geom_bar(stat = "identity", position = dodge, fill="#3399FF") + geom_errorbar(limits, position = dodge, width = 0.25) + theme(axis.text.x=element_text(angle = 45, hjust=1), axis.ticks.x=element_blank(), axis.text.y=element_text(size = 12), axis.title.y = element_text(size=15), axis.title.x=element_blank(), axis.text=element_text(size=12, colour="black"), plot.title=element_text(size=18, face="bold") ) + ylab("Target/Reference") + ggtitle("ARSA: Liposarcoma cell lines") + geom_text(data=samplev, aes(x=samples, y=Target+0.1, label=Target, vjust=0)) dev.off() 转换为HTML并将其存储在attributedText内。我一直收到错误

[__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象插入nil对象[0]

我检查了我正在使用的词典的值,它们不是零,我不知道问题是什么。

生成属性文本,其值为“#34;您的消息在这里&#34;在预期的红色文本中,但是,当我尝试将其插入此行UITextView时,我得到了该错误。

tx.attributedText = attrString;

编辑:我已经简化了代码,我已经删除了Web请求,只是插入了简单的html。无论插入什么,我仍然会得到同样的错误

这是 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; 的原始值:我可能搞砸了这个值

htmlString

"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="Content-Style-Type" content="text/css"><title></title><meta name="Generator" content="Cocoa HTML Writer"><style type="text/css">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}</style></head><body><p class="p1"><span class="s1">Your Message Here!</span></p</body></html>""

options

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

attrString

使用`attrString NSConcreteAttributedString * @"hi test html"

其中attributes NSRLEArray * 0x7fa0fd8053a0 0x00007fa0fd8053a0NSObject

1 个答案:

答案 0 :(得分:0)

出于某种原因,如果没有在主线程上运行,上面的代码工作正常我必须将textView传递给函数并将html转换为其中的attributedText

    - (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;
}