Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ternary operator for tableCell description strings?

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
                }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading