我有这样的视图:
struct PageView: View {
var articulos : [DetallesArticulo]
var altura : CGFloat
var body: some View {
if(articulos.count > 0)
{
TabView {
ForEach(0...articulos.count-1, id: .self) {i in
let vistaTotal = ArticulosViewMinimum()
VStack{
//Code
}.id(UUID())
}
.padding(.all, 10)
}
.frame(width: UIScreen.main.bounds.width, height: altura)
.tabViewStyle(PageTabViewStyle())
}
}
}
ArticulosViewMinimum是这样的视图:
class ArticulosViewMinimum: UIView{
required init(){
super.init(frame: CGRect.zero)
self.backgroundColor = .white
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
self.addSubview(sampleText)
}
lazy var sampleText : UITextView = {
let text = UITextView()
text.attributedText = "sample text".htmlToAttributedString(size: 16, titulo: "")
return text
}()
}
它崩溃并显示以下错误消息:
[error] precondition failure: setting value during update: 51768配置:DYLD_LIBRARY_PATH =/usr/lib/系统/自省DYLD_INSERT_LIBRARIES =/开发/usr/lib/libBacktraceRecording.dylib:/开发/usr/lib/libMainThreadChecker.dylib:/开发/图书馆/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylibAttributeGraph前置条件失败:更新时设置的值:51768 .
它崩溃在我的函数(扩展字符串)转换HTML字符串NSAttributedString:
func htmlToAttributedString(size: CGFloat) -> NSAttributedString? {
var attributedString = NSAttributedString()
if(!self.isEmpty)
{
var htmlTemplate = """
<!doctype html>
<html>
<head>
<style>
body {
font-family: -apple-system;
font-size: (size)px;
}
</style>
</head>
<body>
(self)
</body>
</html>
"""
guard let data = htmlTemplate.data(using: .utf8) else { return NSAttributedString()}
do {
attributedString = try NSAttributedString(
data: data,
options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
],
documentAttributes: nil)
} catch {}
}
return attributedString
}
知道是什么导致了坠机吗?
最后,我通过使用另一个线程来执行初始化代码来解决问题。我不太明白为什么它会起作用,但它确实起作用了。
我读到这个问题是一个快速的错误,我希望在下次更新他们解决它。
class ArticulosViewMinimum: UIView{
required init(){
super.init(frame: CGRect.zero)
self.backgroundColor = .white
DispatchQueue.main.async //<- Now works
{
setupViews()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews(){
self.addSubview(sampleText)
}
lazy var sampleText : UITextView = {
let text = UITextView()
text.attributedText = "sample text".htmlToAttributedString(size: 16, titulo: "")
return text
}()
}