如何使用 swift 中的 json 数据检查 "switch" 语句
How to check "switch" statements with json data in swift
我需要检查 for 循环中的多个 if 条件,所以我不能那样检查,所以如何使用下面代码中的 switch
语句
code: 这里如何将下面的代码变成 switch
语句。请指导我
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: 这里来自用户名 Vikas, New
还需要显示 cell.serviceNameLbl.text
但不显示原因
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
你可以像这样使用 Switch 语句
switch self.keyword {
case oneSerdet.sub_category?.title:
break
case oneSerdet.category?.title:
break
default:
break
}
我需要检查 for 循环中的多个 if 条件,所以我不能那样检查,所以如何使用下面代码中的 switch
语句
code: 这里如何将下面的代码变成 switch
语句。请指导我
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: 这里来自用户名 Vikas, New
还需要显示 cell.serviceNameLbl.text
但不显示原因
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
你可以像这样使用 Switch 语句
switch self.keyword {
case oneSerdet.sub_category?.title:
break
case oneSerdet.category?.title:
break
default:
break
}