Home > database >  Ternary operator for tableCell description strings?
Ternary operator for tableCell description strings?

Time:12-01

just want to simplify the else statement with a ternary expression, because the description is the only change in the code

if LaunchDarklyEnvironment.isPromoCodeAfterRegistrationForExistingUserEnabled() == true {
                    let descriptionCell = tableView.dequeueReusableCell(withIdentifier: ScoreCardStyledPrimaryTableViewCell.reuseID) as? ScoreCardStyledPrimaryTableViewCell
                    descriptionCell?.configure(withTitle: "promo_code_after_registration_description_header".localized, description: "promo_code_after_registration_description_short".localized)
                    cell = descriptionCell
                } else{
                    let descriptionCell = tableView.dequeueReusableCell(withIdentifier: ScoreCardStyledPrimaryTableViewCell.reuseID) as? ScoreCardStyledPrimaryTableViewCell
                    descriptionCell?.configure(withTitle: "promo_code_after_registration_description_header".localized, description: "promo_code_after_registration_description".localized)
                    cell = descriptionCell
                }

CodePudding user response:

It looks like the only thing you're changing is in the descriptionCell?.configure section?

If so, one of the easier ways to do it is to establish a few variables first:

let shortConfiguration = "promo_code_after_registration_description_short".localized
let normalConfiguration = "promo_code_after_registration_description".localized

Then, you can use the ternary operation in the descriptionCell configuration:

let descriptionCell = tableView.dequeueReusableCell(withIdentifier: ScoreCardStyledPrimaryTableViewCell.reuseID) as? ScoreCardStyledPrimaryTableViewCell
                descriptionCell?.configure(withTitle: "promo_code_after_registration_description_header".localized, description: LaunchDarklyEnvironment.isPromoCodeAfterRegistrationForExistingUserEnabled() ? shortConfiguration : normalConfiguration) // <-- Add it right here
                cell = descriptionCell

(You don't need to add " == true", given that the conditional will look for that anyway, so just adding the function (which I assume returns a Bool) is enough to get the ternary going)

  • Related