简体   繁体   English

如何在pickerview中获取(使用的struct:codable)解析字符串的结果

[英]How to get result of ( used struct:codable) parsing string in a pickerview

I have hit the API and got the response successfully. 我点击了API并成功获得响应。 I used codable here for parsing.1.But could not get a particular value. 我在这里使用codable进行解析1.但是无法获得特定的值。 id? ID? 2. The picker is called first and not loaded with the strings which we parsed. 2.首先调用选择器,而不加载我们解析的字符串。

Parsing from codable is littly sticky. 从可编码解析有点粘。 How to get the array content inside it , like the id value into it. 如何获取其中的数组内容,例如将id值放入其中。
codable part: struct Reasons : Codable{ let reason_and_whom_meet : both? 可编码部分:struct原因:可编码{让reason_and_whom_meet:两者? } }

struct both :Codable{
   let reason_list : [reasonlist]?
   let whom_meet : [nameList]?
}

struct reasonlist :Codable{
  let id: Int?    //This "id" i couldnt access. 
  let reason : String?
  enum CodingKeys: String, CodingKey {

  case reason
  case id
  }
}

struct nameList :Codable{
    let name : String?
    enum CodingKeys: String, CodingKey{
        case name
    }
}

The codable struct I did by the structure of json which i receive. 我通过接收到的json的结构完成的可编码结构。 Should I change the structure of the struct I have used. 我是否应该更改所用结构的结构。

If you want the json value also I can provide, but the struct is exact hierarchy of json. 如果您还想要json值,我也可以提供,但该结构是json的确切层次结构。

Code Part: 代码部分:

do {
    let gotData = try JSONDecoder().decode(Reasons.self, from: data!)
    let persons = gotData.reason_and_whom_meet?.whom_meet

    self.wtmArray.add(persons)
    print(self.wtmArray)

    let reasons = gotData.reason_and_whom_meet?.reason_list
    self.reasonArray.add(reasons)
    print(self.reasonArray)

    let reasonID = gotData.reason_and_whom_meet?.reason_list![self.reasonID]
    print(reasons)
} catch let err {
    print("Error", err)
}

Also the picker delegate methods are called first but the response is empty at that time. 选择器委托方法也首先被调用,但是那时响应为空。 How to resolve this? 如何解决呢? My picker shows empty. 我的选择器显示为空。 And the response are like : 和响应就像:

"GoogleClassroomPoC.nameList(name: Optional(\"Teacher\"))"

But I need that Teacher and Principal alone in my array. 但是我需要我的老师和校长一个人。 Should I convert that to any format or can we show it directly in pickerview. 我应该将其转换为任何格式,还是可以直接在pickerview中显示。

I am fighting with this parsing, Please help! 我正在与这个解析战斗,请帮助!

I have created a simple code that is similar to your scenario on how to get this working. 我创建了一个简单的代码,该代码类似于您如何实现此功能的方案。

1. A simple UIPickerView with a single component that display reason of reason_list of both . 1.具有单个组件的简单UIPickerView ,显示both组件的reason_listreason

var data: both?

@IBOutlet weak var picker: UIPickerView!

override func viewDidLoad()
{
    super.viewDidLoad()
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return self.data?.reason_list?.count ?? 0
}

func numberOfComponents(in pickerView: UIPickerView) -> Int
{
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    return self.data?.reason_list?[row].reason
}

Initially, when the ViewController is loaded, data is nil and so the UIPickerView doesn't show anything. 最初,加载ViewController时, datanil ,因此UIPickerView不显示任何内容。

2. I have created a UIButton that loads the data and the UIPickerView is reloaded. 2.我创建了一个UIButton来加载data然后重新加载UIPickerView I've used some hardcoded data. 我使用了一些硬编码的数据。 You can get this data from your API. 您可以从API获取此数据。

@IBAction func loadData(_ sender: UIButton)
{
    let jsonString = """
    {
        "reason_list": [
            {"id": 1, "reason": "ABCD"},
            {"id": 2, "reason": "WXYZ"}
        ]
    }
    """

    if let data = jsonString.data(using: .utf8)
    {
        self.data = try? JSONDecoder().decode(both.self, from: data)
         DispatchQueue.main.async {[weak self] in
               self?.picker.reloadComponent(0) //HERE..!!!
         }
    }
}

Let me know if you still face any issues. 让我知道您是否仍然遇到任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM