I'm trying to make an app that fetches blog posts from a Wordpress website. And that works, the title and image work fine. Only when I add the content, the content is between the <p>
and </p>
when I run it in the simulator.
What am I doing wrong? How can I get the content without the p
's?
This is my code:
import SwiftUI
import SDWebImageSwiftUI
struct ContentView: View {
@State private var articles : [BeholdArticle] = []
var body: some View {
NavigationView{
ScrollView {
VStack (alignment: .center) {
ForEach(articles) { article in
Text(article.title.rendered)
.multilineTextAlignment(.leading)
.padding(.horizontal)
.background(.green)
.font(.largeTitle)
.cornerRadius(10)
Text(article.content.rendered)
.multilineTextAlignment(.leading)
.padding(.horizontal)
//.background(.blue)
.font(.headline)
if let thumbnailURL = URL(string: article.thumbnail) {
WebImage(url: thumbnailURL)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width:350)
.padding(.horizontal)
}
}
}
}
.onAppear {
loadArticles()
}
}
}
private func loadArticles() {
guard let url = URL(string: "https://firmofthefuture.shop/wp-json/wp/v2/posts") else {
return
}
URLSession.shared.dataTask(with: url) {data, response, error in
guard let data = data else { return }
if let decodedData = try? JSONDecoder().decode([BeholdArticle].self, from: data){
DispatchQueue.main.async {
self.articles = decodedData
}
}
}.resume()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
struct BeholdArticle: Decodable, Identifiable {
var id: Int
var slug: String
var link: String
var thumbnail: String
var title: BeholdArticleTitle
var content: BeholdArticleContent
enum CodingKeys: String, CodingKey {
case thumbnail = "jetpack_featured_media_url"
case slug, link, title, content
case id = "id"
}
}
struct BeholdArticleTitle: Decodable {
var rendered: String
}
struct BeholdArticleContent: Decodable {
var rendered: String
}
Text
element. Another would be to transform theString
and remove the HTML entities. - jnpdx