1 year ago
#370927
Daniel Jeong
Swift: Code builds but doesn't work—preview and simulator don't display desired result
I can't seem to find any problems with my code, and it builds, but it doesn't show anything I've coded. I will attach my code for reference below. Any help with this would be greatly appreciated. Thank you so much in advance.
ArticleRowView.swift
import SwiftUI
fileprivate let relativeDateFormatter = RelativeDateTimeFormatter()
struct ArticleRowView_Previews: PreviewProvider {
static var previews: some View {
List{
// article: .previewData[0]
ArticleRowView(article: .previewData[0])
.listRowInsets(.init(top:0, leading: 0, bottom: 0, trailing: 0))
}.listStyle(.plain)
}
}
struct ArticleRowView: View {
let article: Article
struct Article {
let site: String
let title: String
let date: Date
let tickers: String
let url: String
let link: String?
let author: String?
let image: String?
var siteText: String {
site
}
var titleText: String {
title
}
var tickerText: String {
tickers
}
var dateText: String {
"\(relativeDateFormatter.localizedString(for: date, relativeTo: Date()))"
}
var imageURL: URL? {
guard let image = image else {
return nil
}
return URL(string: image)
}
}
struct NewsAPIResponse: Decodable {
let content: [Article]
}
var body: some View {
VStack(spacing: 20){
HStack(spacing: 16){
VStack(alignment: .leading, spacing: 4){
Text(article.siteText)
.font(Font.custom("Inter-Regular", size: 12))
Text(article.titleText)
.font(Font.custom("Inter-SemiBold", size: 15))
.frame(width: 247)
.padding([.bottom], 4)
HStack(spacing: 10){
Text(article.dateText)
.font(Font.custom("Inter-Regular", size: 12))
Image("rect")
Text(article.tickerText)
Image("green")
.padding([.leading, .trailing], -8)
Text("2.33%")
.foregroundColor(Color(red: 0.345, green: 0.50, blue: 0.155, opacity: 1.0))
}
}
AsyncImage(url: article.imageURL){
phase in
switch phase{
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
case .failure:
Image(systemName: "photo")
@unknown default:
fatalError()
}
}
}
Image("rect-hor")
}
}
}
extension ArticleRowView.Article: Codable{}
extension ArticleRowView.Article: Equatable{}
extension ArticleRowView.Article: Identifiable{
var id: String{url}
}
extension ArticleRowView.Article {
static var previewData: [ArticleRowView.Article] = { () -> [ArticleRowView.Article] in
let previewDataURL = Bundle.main.url(forResource: "news", withExtension: "json")!
let data = try! Data(contentsOf: previewDataURL)
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .iso8601
let apiResponse = try!jsonDecoder.decode(ArticleRowView.NewsAPIResponse.self, from: data)
return apiResponse.content ?? []
}()
}
---
I have attached the news.json below for reference for endpoints.
news.json
{
"content": [
{
"title": "Core & Main, Inc Reports Strong Beat",
"date": "2022-03-31 11:26:00",
"content": "<p><a href='https://financialmodelingprep.com/financial-summary/CNM'>Core & Main, Inc. (NYSE:CNM) </a>reported its Q4 results, with EPS of $0.28 beating the consensus estimate of $0.22. Revenue was $1.25 billion, compared to the consensus estimate of $1.12 billion.</p>\n<p>The company unveiled its full 2022-year adjusted EBITDA guidance, which was well ahead of consensus estimates. The company expects Adj. EBITDA to range from $595 million to $635 million with an implied margin contraction of 80–90bp.</p>\n<p>Analysts at Berenberg Bank said they have limited visibility into how long the current hyperinflationary trends experienced in the company’s commodity-based products can persist and therefore acknowledge there could be an upside risk to their near-term estimates. Nonetheless, it is likely inevitable for these same commodity-based products to begin experiencing equally sharp deflationary pressures, resulting in more normalized margins and, by extension, muted medium-term EBITDA growth. Consequently, the analysts leave their $27 price target and Hold rating unchanged.</p>\n",
"tickers": "NYSE:CNM",
"image": "https://cdn.financialmodelingprep.com/images/fmp-1648740380438.jpg",
"link": "https://financialmodelingprep.com/market-news/fmp-core-&-main-inc-reports-strong-beat",
"author": "Davit Kirakosyan",
"site": "Financial Modeling Prep"
},
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"pageSize": 5,
"pageNumber": 0,
"offset": 0,
"paged": true,
"unpaged": false
},
"totalPages": 161,
"totalElements": 803,
"last": false,
"number": 0,
"size": 5,
"numberOfElements": 5,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"first": true,
"empty": false
}
swift
swiftui
preview
0 Answers
Your Answer