只想用三元運算式簡化else陳述句,因為描述是代碼中唯一的變化
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
}
uj5u.com熱心網友回復:
看起來您要更改的唯一內容是在 descriptionCell?.configure 部分?
如果是這樣,一種更簡單的方法是首先建立幾個變數:
let shortConfiguration = "promo_code_after_registration_description_short".localized
let normalConfiguration = "promo_code_after_registration_description".localized
然后,您可以在 descriptionCell 配置中使用三元操作:
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
(您不需要添加“ == true”,因為條件無論如何都會尋找它,所以只需添加函式(我假設它回傳一個 Bool)就足以讓三元運行)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371292.html
上一篇:將字串中的元音轉換為大寫
下一篇:如何將JSON解碼成不同的型別?
