Hey Guys, I have a problem with the RichText Object in Swift UI:
on my console i get this output
[“orderId”: 2, “content”: <Contentful.RichTextDocument: 0x6000039e1b80>, “subtitle”: “Grundlagen, System, Templates”, “created”: “2021-06-06T00:00”, “title”: “Dokumentieren”, “showcontent”: true, “logo”: “scribble.variable”]
So connection and retrieving works fine, but now comes the problem.
This is my code:
import SwiftUI
import Contentful
import ContentfulRichTextRenderer
import UIKit
// My struct looks like this:
struct Cards: Identifiable {
let id = UUID()
var title: String
var subtitle: String
var image: String
var logo: String
var content: String
var created: String
var show: Bool
var orderId: Int
}
class CardsStore: RichTextViewController, ObservableObject{
@Published var cards: [Cards] = sectionCards
// I have masked this
let client = Client(spaceId: "xxx", accessToken: "xxx")
@State var htmlContent: String = "★"
@State var tmpHtml: String = ""
init(){
let configuration = DefaultRendererConfiguration()
let renderersProvider = DefaultRenderersProvider()
let renderer = RichTextDocumentRenderer(
configuration: configuration,
nodeRenderers: renderersProvider
)
super.init(renderer: renderer)
//testContenful()
getArray(id: "chapterCards") { (items) in
items.forEach { (item) in
print(item.fields)
self.cards.append(Cards(
title: item.fields["title"] as! String,
subtitle: item.fields["subtitle"] as! String,
image: item.fields["image"] != nil ? item.fields["image"] as! String : "photo.fill",
logo: item.fields["logo"] as! String,
**content: item.fields["content"] as! String, // at this point the app crashes**
created: item.fields["created"] as! String,
show: item.fields["show"] != nil ? item.fields["show"] as! Bool : true,
orderId: item.fields["orderId"] != nil ? item.fields["orderId"] as! Int : 999
)
)
// sort entries
self.cards.sort {
$0.orderId < $1.orderId
}
}
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func getArray(id: String, completion: @escaping([Entry]) -> ()){
let query = Query.where(contentTypeId: id)
client.fetchArray(of: Entry.self, matching: query){ result in
switch result{
case .success(let array):
DispatchQueue.main.async{
completion(array.items)
}
case .failure(let error):
print(error)
}
}
}
}
So here is my question:
where and how do I have to convert RichText to String, at which point ?
Are there any solution for that ? Can anybody give me an example how to implement this ?
Thx in advance
Chris