Swiftui 如何将通过点击第一个端点接收的值设置为第二个端点中的过滤器?
我目前正在使用SwiftUI开发一个应用程序 我想在视图的两个列表中显示两种数据Swiftui 如何将通过点击第一个端点接收的值设置为第二个端点中的过滤器?,swiftui,Swiftui,我目前正在使用SwiftUI开发一个应用程序 我想在视图的两个列表中显示两种数据 (1) 开始日期 (2) 从开始日期(1)到今天每天的温度 在我的代码中,(1)开始日期显示得很好,但(2)温度有问题,因为每个列表应显示不同的数据,但它们在每个列表中显示相同的数据。。。 虽然我可以检查每个方法何时被调用,但它们在控制台中生成不同的数据,如下所示,模拟器显示相同的数据 控制台中的结果: temp_info 25.7 24.9 temp_info 25.6 25.7 24.9 24.1 23.
- (1) 开始日期
- (2) 从开始日期(1)到今天每天的温度
temp_info
25.7
24.9
temp_info
25.6
25.7
24.9
24.1
23.5
25.7
26.4
23.7
23.0
24.4
26.1
我如何解决这个问题
代码如下: JsonModel.swift
import Foundation
struct DbVegetableInfos: Codable,Identifiable {
var id: Int
var start_date: String
}
struct WeatherAveInfos:Codable,Identifiable {
var id: Int
var ave_temp: Float
}
AppState.swift
import SwiftUI
import Foundation
import Combine
import UIKit
class AppState: ObservableObject {
@Published var arrayDbVegetableInfos:[DbVegetableInfos]?
@Published var weatherAveInfos:[WeatherAveInfos]?
func makeGetCallVegetableInfos() {
// Set up the URL request
let endpoint: String = "https://sample.com/api/info/"
guard let url = URL(string: endpoint) else {
print("Error: cannot create URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.addValue("token xxxxxxxxxx", forHTTPHeaderField: "authorization")
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
DispatchQueue.main.async {
do{ self.arrayDbVegetableInfos = try JSONDecoder().decode([DbVegetableInfos].self, from: responseData)
}catch{
print("Error: did not decode")
return
}
}
}
task.resume()
}
}
func makeGetCallWeatherAveTemp(start_date:String ) {
// Set up the URL request
let endpoint: String = "https://sample.com/api/weather_ave/?start_date=\(start_date)"
guard let url = URL(string: endpoint) else {
print("Error: cannot create URL")
return
}
var urlRequest = URLRequest(url: url)
urlRequest.addValue("token xxxxxxxxxx", forHTTPHeaderField: "authorization")
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET")
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
DispatchQueue.main.async {
do{ self.weatherAveInfos = try JSONDecoder().decode([WeatherAveInfos].self, from: responseData)
print("temp_info")
for info in self.weatherAveInfos!{
print(info.ave_temp)
}
}catch{
print("Error: did not decode")
return
}
}
}
task.resume()
}
HomeView.swift
import SwiftUI
struct HomeView: View {
@EnvironmentObject var appState: AppState
var body: some View {
NavigationView{
VStack{
ForEach(appState.arrayDbVegetableInfos ?? []){ info in
VStack{
VegetableInfoRow(info:info)
}.background(Color(.secondarySystemFill))
.cornerRadius(10)
.padding(.top)
.padding(.leading)
.padding(.bottom)
}
}.onAppear(){
appState.makeGetCallVegetableInfos()
}
}
}
}
VegetableInfoRow.swift
import SwiftUI
struct VegetableInfoRow: View {
@EnvironmentObject var appState: AppState
var info:DbVegetableInfos
var body: some View {
ScrollView(.horizontal) {
HStack{
VStack{
VStack{
Text("start_date:").padding()
Text(stringToStringDate(stringDate: info.start_date, format: "yyyy-MM-dd"))
}
}
Divider()
.padding()
VStack{
VStack{
Text("progress_temperature:").padding()
ForEach(appState.weatherAveInfos ?? []){ info in
Text(String(info.ave_temp))
}
}
}
}
}.onAppear(){
appState.makeGetCallWeatherAveTemp(start_date: info.start_date)
}
}
}
func stringToStringDate(stringDate: String, format:String) -> String {
let formatter: DateFormatter = DateFormatter()
formatter.calendar = Calendar(identifier: .gregorian)
formatter.dateFormat = "yyyy/MM/dd"
let newDate = formatter.date(from: stringDate)!
formatter.dateFormat = format
return formatter.string(from: newDate)
}
Xcode:Version 12.0.1您需要在两个模型之间建立某种连接objects@JoakimDanielson,这些数据来自
DRF
API,那么,我必须在Django
侧的模型中连接吗?我不知道什么是DRF或Django,但您正在下载给定开始日期的天气数据,但每次下载时,您都会在AppState中覆盖weatherAveInfos的内容,而您需要在本地以某种方式存储每个开始日期的数据