【发布时间】: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 的原始值:我可能在粘贴值时搞砸了
"<!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 0x00007fa0fd8053a0
里面是NSObject 的isa Class NSRLEArray 0x000000010a03ede8
【问题讨论】:
-
您正在尝试将 nil 插入字典;不是一个人。
-
通过提供的信息真的很难猜出你的问题,你能提供异常发生时的堆栈吗?
-
提供实际的原始 JSON 也可能会有所帮助。
-
请给我们应用程序停止执行的行。您的代码中有一个字典文字,但它不太可能是崩溃的那个。
-
请注意,我们通常不会分配/初始化 NSError 参数。没必要。
标签: objective-c uitextview nsdictionary nsattributedstring