Home > front end >  Unable to check Switch cases with "null" value in swift
Unable to check Switch cases with "null" value in swift

Time:06-07

To check multiple conditions i am using switch statements

code: here all cases working fine but. if user_details?.rating = "" is empty then i need to show default case i mean all stars empty but its going to case _ where user_details?.rating ?? "" <= "1.9" why?

if i do force unwrap case _ where (user_details?.rating)! <= "1.9": then app getting crash when reach this

user_details?.rating = "" then how to show all stars empty

please guide me

switch user_details?.rating{

case "1.0":

    star1.image = UIImage(named: "star")

    star2.image = UIImage(named: "staremp")
    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
break
case _ where user_details?.rating ?? "" <= "1.9":

    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "star-half")

    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
break

case "2.0":

    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "star")

    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")

 break

case _ where user_details?.rating ?? "" <= "2.9":

    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "star")
    star3.image = UIImage(named: "star-half")

    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
 break


default:
    star1.image = UIImage(named: "staremp")
    star2.image = UIImage(named: "staremp")
    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")

break

}

CodePudding user response:

The app is crashing because you are force unwrapping user_details?.rating which basically says: This property holds a value that is not nil, and if I'm wrong crash the app.

Try unwrapping user_details?.rating safely before the switch statement:

  1. Using if let:

    if let rating = user_details?.rating {
    }else {
    }
    
  2. Using guard let:

    guard let rating = user_details?.rating else {
        return
    }
    
  3. Providing default value:

    let rating = user_details?.rating ?? ""
    

Then your code will look like:

guard let rating = user_details?.rating else {
    //do something if it is nil
    return
}

switch rating {
case "1.0":
    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "staremp")
    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
    break
case _ where user_details?.rating ?? "" <= "1.9":
    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "star-half")
    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
    break
case "2.0":
    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "star")
    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
    break
case _ where user_details?.rating ?? "" <= "2.9":
    star1.image = UIImage(named: "star")
    star2.image = UIImage(named: "star")
    star3.image = UIImage(named: "star-half")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
    break
default:
    star1.image = UIImage(named: "staremp")
    star2.image = UIImage(named: "staremp")
    star3.image = UIImage(named: "staremp")
    star4.image = UIImage(named: "staremp")
    star5.image = UIImage(named: "staremp")
    break
}
  • Related