简体   繁体   English

如何将数据从一个 class 发送到 SwiftUI 中的另一个?

[英]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.

相关问题 如何从另一个 class 编辑 SwiftUI 中的 ObservableObject 的属性? - How do you edit an ObservableObject’s properties in SwiftUI from another class? 您可以从一个班级发送一条消息来触发另一个班级的“自我”吗? - Can you send a message from one class to trigger a 'self' in another? 如何将数据从一个 SwiftUI 视图文件传递到另一个? - How do I pass data from one SwiftUI view file to another? 如何将 Json 数据从一个 class 传递到另一个? - How do I pass Json Data from one class to another? 尝试将数据从一个类发送到另一类ios(解析) - Trying to send data from one class to another class ios (parse) 在iOS5中使用Storyboard时,如何将数据从一个文本字段传递到另一个文本字段? - How do you pass data from one text field to another when using Storyboards in iOS5? 如何将解析对象字段从一类发送到另一类? - How to send Parse object field from one class to another? 如何使用 rxswift 将信息从一个 class 发送到另一个? - How to send information from one class to another using rxswift? 从一个类发送UIImage到另一个 - send UIImage from one class to another 如何将数据从一个ViewController发送到另一个具有SWRevealViewController的视图 - how to send data from one ViewController to another which has SWRevealViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM