Home > Back-end >  Unable to check JSON response condition to show data in tableview cell in swift
Unable to check JSON response condition to show data in tableview cell in swift

Time:07-30

this is json response in postman:

{
"result": {
 
    "categories": [
        {
            "title": "ADMINISTRATION & SECRETARIAL",
            "children": [
                {
                    "id": 266,
                    "certificate_required": "Y",
                    "get_service": null
                }
            ]
        },
        {
            "title": "BUILDING",
            "children": [
                {
                    "id": 299,
                    "certificate_required": "Y",
                    "get_service": {
                        "id": 778,
                        "get_certificate": {
                           
                            "certificate_file": "62e3a911d0233.jpg",
                           
                        }
                    }
                }
            ]
        },
        {
            "id": 148,
            "title": "DIGITAL DEVELOPMENT ",
            "children": [
                {
                    "id": 152,
                    "title": "WEB DESIGN",
                    "certificate_required": "N",
                    "get_service": null
                }
            ]
        }
    ]
}
}

here i need to check get_certificate is there or not

so i have written code like below:

here if

get_certificate != nil 

then i need to show

cell.subCatLbl.text = "\(indexData?.title ?? "") (Uploaded)

but still showing

cell.subCatLbl.text = "\(indexData?.title ?? "") (Certificate). 

why?

where am i wrong. please guide me

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categoryData[section].children?.count ?? 0
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AddserviceCell", for: indexPath) as! AddserviceCell
cell.selectionStyle = .none

let indexData = categoryData[indexPath.section].children?[indexPath.row]

switch indexData {
case _ where indexData?.certificate_required == "Y":
    cell.subCatLbl.text = "\(indexData?.title ?? "") (Certificate)"
case _ where ((indexData?.get_service?.get_certificate) != nil):
    cell.subCatLbl.text = "\(indexData?.title ?? "") (Uploaded)"
default:
    cell.subCatLbl.text = indexData?.title
    break
}
return cell
}

CodePudding user response:

You are not achieving the outcome you expect because of your switch statement.

The switch statement execute only one of its paths, the first matching condition to be precise. So every time

indexData?.certificate_required == "Y"

is true

cell.subCatLbl.text = "\(indexData?.title ?? "") (Certificate)"

will execute regardless of what comes next.

Solution:

Either resort the switch condition so that the condition with the highest priority comes first:

case _ where ((indexData?.get_service?.get_certificate) != nil):
    cell.subCatLbl.text = "\(indexData?.title ?? "") (Uploaded)"
case _ where indexData?.certificate_required == "Y":
    cell.subCatLbl.text = "\(indexData?.title ?? "") (Certificate)"
default:
    cell.subCatLbl.text = indexData?.title
    break

or use an if/else approach here:

if indexData?.get_service?.get_certificate != nil && indexData?.certificate_required == "Y"{
    cell.subCatLbl.text = "\(indexData?.title ?? "") (Uploaded)"
} else if indexData?.certificate_required == "Y"{
    cell.subCatLbl.text = "\(indexData?.title ?? "") (Certificate)"
} else {
    cell.subCatLbl.text = indexData?.title
}
  • Related