JASON response like this: I am showing services in dropdown...now i need selected 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,
},
EDIT: swift class model for JSON response 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?
}
Code: I am able to show result in dropdown and selected service name in textfield but i need selected category_id
how ? if i give like this in for loop selectedCategoryId = totData.category_id
all the time i am getting 11.. why? how do i get selected 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()
}
}
how do i get selected category_id
please do guid me
CodePudding user response:
you have selectedCategoryId = totData.category_id
inside the loop,
so you will always get the last category_id
of your autosearchResult?.result?.data
. The question is, which category_id
do you want to select?
Move selectedCategoryId
outside the for loop
, and pick the one
you want using, for example something like:
if let result = autosearchResult?.result,
let selectedCategoryId = result.data.first(where: {$0.category_id == x})
{
print(selectedCategoryId)
}
EDIT1:
To use the textfield text for category_id
, then use something like this:
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)
}
Since I don't have all your code info, you will have to adjust the code accordingly.
CodePudding user response:
use this function to get selected category data.
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})
}
you can use this as
var filterData = getCategoryData(data:originalDataArray, categoryID:2)
-> Some Suggestions you can you Codable object to convert you API Response to Object directly.