Bjorn
Well-known member
Nee geen problemen, ben hierna ook gestopt dus mss als ik nog verder bleef vragen ?!Had hij bij u geen issues? Ik heb precies al wat vaker dat wanneer hij een grotere hoeveelheid code moet teruggeven dat hij op een gegeven moment stopt met een output te geven of die Body Stream error geeft.
Nog wat verder zitten mee spelen, echt geinig Hij verstaat zelfs mijn brakke engels. Voor iemand die een beetje bekend is met een specifieke programmeertaal is dit wel een time saver. En dan te denken dat het nog in zijn kinderschoenen staat.
Ik begon dus met de vraag: "Write me some Swiftui code that fetches some articles in a scrolling list. The article needs to include the title, body, and date of publishing."
Swift:
import SwiftUI
struct Article: Decodable {
let title: String
let body: String
let date: Date
}
struct ArticlesView: View {
@State private var articles: [Article] = []
var body: some View {
List(articles) { article in
VStack(alignment: .leading) {
Text(article.title)
.font(.headline)
Text(article.body)
.font(.body)
Text(article.date, style: .date)
.font(.caption)
}
}
.onAppear {
// Fetch the articles here and update the articles array
}
}
}
Volgende vraag: Include a function that fetches the article in JSON format from an example API.
Swift:
import SwiftUI
struct Article: Decodable {
let title: String
let body: String
let date: Date
}
struct ArticlesView: View {
@State private var articles: [Article] = []
var body: some View {
List(articles) { article in
VStack(alignment: .leading) {
Text(article.title)
.font(.headline)
Text(article.body)
.font(.body)
Text(article.date, style: .date)
.font(.caption)
}
}
.onAppear {
self.fetchArticles()
}
}
func fetchArticles() {
let url = URL(string: "https://www.example.com/api/articles")!
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let articles = try! JSONDecoder().decode([Article].self, from: data)
DispatchQueue.main.async {
self.articles = articles
}
}
}.resume()
}
}
Next: Use gnews API
Swift:
import SwiftUI
struct Article: Decodable {
let title: String
let body: String
let date: Date
}
struct ArticlesView: View {
@State private var articles: [Article] = []
var body: some View {
List(articles) { article in
VStack(alignment: .leading) {
Text(article.title)
.font(.headline)
Text(article.body)
.font(.body)
Text(article.date, style: .date)
.font(.caption)
}
}
.onAppear {
self.fetchArticles()
}
}
func fetchArticles() {
let url = URL(string: "https://gnews.io/api/v3/search?q=apple&token=your-api-token")!
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let articles = try! JSONDecoder().decode([Article].self, from: data)
DispatchQueue.main.async {
self.articles = articles
}
}
}.resume()
}
}
Next: Use codingkeys for the date to match the API's publishedAt
Swift:
import SwiftUI
struct Article: Decodable {
let title: String
let body: String
let date: Date
private enum CodingKeys: String, CodingKey {
case title, body, date = "publishedAt"
}
}
struct ArticlesView: View {
@State private var articles: [Article] = []
var body: some View {
List(articles) { article in
VStack(alignment: .leading) {
Text(article.title)
.font(.headline)
Text(article.body)
.font(.body)
Text(article.date, style: .date)
.font(.caption)
}
}
.onAppear {
self.fetchArticles()
}
}
func fetchArticles() {
let url = URL(string: "https://gnews.io/api/v3/search?q=apple&token=your-api-token")!
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let articles = try! JSONDecoder().decode([Article].self, from: data)
DispatchQueue.main.async {
self.articles = articles
}
}
}.resume()
}
}
Laatst bewerkt: