tableCell 描述字符串的三元运算符?
Ternary operator for tableCell description strings?
只是想用三元表达式简化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
}
您似乎唯一要更改的是 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”,因为条件无论如何都会寻找它,所以只需添加函数(我假设 returns a Bool)就足以得到三元的)
只是想用三元表达式简化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
}
您似乎唯一要更改的是 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”,因为条件无论如何都会寻找它,所以只需添加函数(我假设 returns a Bool)就足以得到三元的)