[英]How do you send data from one class to another in SwiftUI?
I currently have two classes in my app.我的应用程序目前有两个课程。
One class stores important values in variables (ex. size, color, width) that correspond to user inputs.一个 class 将重要值存储在与用户输入相对应的变量(例如大小、颜色、宽度)中。
Another class initiates a URLRequest to an API in order to fetch data.另一个 class 向 API 发起 URLRequest 以获取数据。
My goal is to make a parameter in the URL change depending on the values stored in the variable of the first class.我的目标是根据存储在第一个 class 变量中的值更改 URL 中的参数。
For instance, the URL looks like this: "www.google.com/docs/(VAR)" I made VAR into a variable in the class with an API request and I'm trying to mutate it with conditional statements using variables from the first class.例如,URL 看起来像这样:“www.google.com/docs/(VAR)”我将 VAR 设置为 class 中的一个变量,其中 API 使用条件请求的变量和 Imutate it'm 语句第一个 class。
Unfortunately, this is giving me an error and I am unable to transfer data between two classes.不幸的是,这给了我一个错误,我无法在两个类之间传输数据。
Any suggestions?有什么建议么?
Here is the code for the first class:这是第一个 class 的代码:
import Foundation
class Product: ObservableObject, CustomStringConvertible {
@Published var color = ""
@Published var size = ""
@Published var finish = ""
@Published var cut = ""
@Published var length = ""
@Published var type = ""
var description: String {
"\(size) - \(finish) - \(cut) -\(length)"
}
}
Here is the code for the APIRequest这是 APIRequest 的代码
import Foundation
class APIRequest: ObservableObject {
@Published
var lengthOptions: [String] = []
init () {
fetchLengths { lengths in
self.lengthOptions = lengths[0].OptionList.map { $0.OptionName }
}
}
private func fetchLengths (completion: @escaping ([OptionsHead]) -> ()) {
let catalogID = 1877
// how can I change catalogID based on Product class variables
guard let url = URL(string: "blablaAPI.com/Products/\(catalogID)/Options?limit=1&offset=3")
else {
fatalError("URL is not correct")
}
var request = URLRequest(url: url)
request.addValue("12345", forHTTPHeaderField: "PrivateKey")
request.addValue("https://www.blaaaaa.com", forHTTPHeaderField: "SecureURL")
request.addValue("12345", forHTTPHeaderField: "Token")
request.httpMethod = "GET" // "POST", "PUT", "DELE"
URLSession.shared.dataTask(with: request) { data, _, _ in
let outputs = try!
JSONDecoder().decode([OptionsHead].self, from: data!)
DispatchQueue.main.async {
completion(outputs)
}
}.resume()
}
}
you could try passing the Product into you APIRequest class, to change the catalogID based on Product class variables, like this:您可以尝试将产品传递给您的 APIRequest class,以根据产品 class 变量更改目录 ID,如下所示:
class APIRequest: ObservableObject {
@Published var lengthOptions: [String] = []
@ObservedObject var product: Product
init (product: Product) {
self.product = product
fetchLengths { lengths in
self.lengthOptions = lengths[0].OptionList.map { $0.OptionName }
}
}
private func fetchLengths (completion: @escaping ([OptionsHead]) -> ()) {
var catalogID = 1877
// how can I change catalogID based on Product class variables
if product.type == "some type" {
catalogID = 1234
} else {
catalogID = 4321
}
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.