【问题标题】:How do i get dropdown's selected JSON category ID in swift如何快速获取下拉列表中选择的 JSON 类别 ID
【发布时间】:2022-01-21 20:10:05
【问题描述】:

这样的 JASON 响应:我在下拉列表中显示服务...现在我需要选择 category_id

{
 "result": {
     "data": [
         {
             "id": 167,
             "service_name": "Seo",
             "category_id": 1,
         },
         {
             "id": 166,
             "service_name": "Development",
             "category_id": 11,
         },
         {
             "id": 164,
             "service_name": "ghah",
             "category_id": 9,
         },

编辑: JSON 响应的快速类模型AutoSearchResultModel

public class AutoSearchResultModel {
public var result : AutoSearchResult?
 }
public class AutoSearchResult {
public var data : Array<SearchData>?
}
public class SearchData {
public var id : Int?
public var service_name : String?
public var category_id : Int?
}

      

代码:我可以在下拉列表中显示结果并在文本字段中选择服务名称,但我需要选择 category_id 怎么做?如果我在 for loop selectedCategoryId = totData.category_id 中一直这样给出,我得到 11 .. 为什么?我如何获得选定的 id

 var selectedCategoryId: String? = ""
 private var autosearchResult = AutoSearchResultModel(dictionary: NSDictionary())

 func autoSearchService(){
    
 APIReqeustManager.sharedInstance.serviceCall(param: parameters as [String : Any], method: .post, url: CommonUrl.auto_search) { [weak self] (resp) in

     self?.autosearchResult = AutoSearchResultModel(dictionary: resp.dict as NSDictionary? ?? NSDictionary())

  }
 }


 @objc func textFieldDidChange(_ textField:UITextField) {
 var testArray = [String]()
 var originalDataArray:[String] = Array()

         for totData in autosearchResult?.result?.data ?? []{
 
             originalDataArray.append(totData.service_name)
            selectedCategoryId = totData.category_id// all the time i am getting only 11.. how do i get selected category_id
         }

testArray.removeAll()
 if searchTF.text?.count != 0 {
     for totData in originalDataArray{

         if let wordToSearch = searchTF.text{
             let range = totData.lowercased().range(of: wordToSearch, options: .caseInsensitive, range: nil, locale: nil)
             if range != nil {
                 testArray.append(totData)
             }
         }
     }
 }

 showDropDown(with: testArray, from: textField) { [weak self] (item, index) in
     
     self?.searchTF.text = item
     self?.searchResultService()

 }
 }
 

我如何被选中 category_id 请指导我

【问题讨论】:

  • AutoSearchResultModel 是什么样的?
  • @Vollan,我已经用AutoSearchResultModel 编辑了我的帖子。请检查
  • 仍然缺少模型的 dictionary: resp.dict as NSDictionary? ?? NSDictionary() 部分
  • @Vollan,我在 viewdidload 上方给出了这样的private var autosearchResult = AutoSearchResultModel(dictionary: NSDictionary())...我正在使用autosearchResult 来获取值
  • 为什么你在发布问题时使用两个帐户,这个已经发布了yesterday

标签: arrays json swift for-loop


【解决方案1】:

你在循环中有selectedCategoryId = totData.category_id, 所以你总是会得到你的autosearchResult?.result?.data中的最后一个category_id。问题是,你要选择哪个category_id

selectedCategoryId 移到for loop 之外,然后选择一个 你想使用,例如:

if let result = autosearchResult?.result,
   let selectedCategoryId = result.data.first(where: {$0.category_id == x})
{
    print(selectedCategoryId)
}

编辑1:

要使用category_id 的文本字段文本,请使用如下内容:

if let result = autosearchResult?.result,
   let txt = textField.text,
   let catid = Int(txt),  // <-- convert to int
   let selectedCategoryId = result.data.first(where: {$0.category_id == catid})
{
    print(selectedCategoryId)
}

由于我没有您的所有代码信息,因此您必须相应地调整代码。

【讨论】:

  • 如果我将selectedCategoryId = totData.category_id移到for循环之外..那么我如何才能被选中category_id
  • 我没看懂你的代码
  • 这是什么意思result.data.first(where: {$0.category_id == x})
  • 这意味着,从您的数据数组中,选择您想要的选定category_id。你必须决定你想要哪个,x 只是一个位值,例如,123
【解决方案2】:

使用此函数获取选定的类别数据。

func getCategoryData(data: [YourData], categoryID: Int) -> [YourModel] {
    // is you have converted your data into object 
    return data.filter({$0.category_id = categoryID})
    // if you have Dictionary 
    return data.filter({$0["category_id"] = categoryID})

}

你可以用这个

var filterData = getCategoryData(data:originalDataArray, categoryID:2)

-> 一些建议您可以使用 Codable 对象将您的 API 响应直接转换为对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多