Home > Software design >  How to check "switch" statements with json data in swift
How to check "switch" statements with json data in swift

Time:05-26

I need to check multiple if conditions in for loop so, i am not able to check like that so how to use switch statements with this below code

code: here how to turn below code into switch statements. please guide me

let indexData = searchResult?.result?.search_result?[indexPath.row]


if let servDetls = indexData?.service_details{

for oneSerdet in servDetls{
    
    if self.keyword == oneSerdet.sub_category?.title{
        cell.serviceNameLbl.text = "\(oneSerdet.sub_category?.title?.capitalized ?? "ABC")"
  
    print("keyword: \(self.keyword ?? "") \n here the sub category tite: \(oneSerdet.sub_category?.title ?? "") \n user name: \(indexData?.first_name?.capitalized ?? "")")

    }
    else if self.keyword == oneSerdet.category?.title{
        cell.serviceNameLbl.text = "\(oneSerdet.category?.title?.capitalized ?? "")"
    }
    else{
        cell.serviceNameLbl.text = ""
    }
}
}

o/p: here from the username Vikas, New also need to show cell.serviceNameLbl.text but not showing why

 keyword: ecommerce web site 
 here the sub category tite: ecommerce web site 
 user name: Dips
 keyword: ecommerce web site 
 here the sub category tite: ecommerce web site 
 user name: James
 keyword: ecommerce web site 
 here the sub category tite: ecommerce web site 
 user name: Vikas
 keyword: ecommerce web site 
 here the sub category tite: ecommerce web site 
 user name: Test
 keyword: ecommerce web site 
 here the sub category tite: ecommerce web site 
 user name: New

CodePudding user response:

You can use Switch Statement like this

switch self.keyword {
case oneSerdet.sub_category?.title:
    break
case oneSerdet.category?.title:
    break
default:
    break
}
  • Related