如何进一步缩短三元运算符

时间:2018-10-25 14:14:29

标签: ios swift uitableview ternary-operator

我正在尝试清理代码,目前处于这种三元状态。我想重新整理它并使其简短化。我该如何应对?

let user = AppDelegate.shared.user!.userID
let messageSender = customMessage.sender.userID
let sender = (user == messageSender)

myCell = sender ? tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath) : tableView.dequeueReusableCell(withIdentifier: "secondCell", for: indexPath)

2 个答案:

答案 0 :(得分:3)

看看这个:

let cellName = (user == messageSender) ? "firstCell" : "secondCell"
myCell = tableView.dequeueReusableCell(withIdentifier: cellName, for: indexPath)

答案 1 :(得分:2)

let user = AppDelegate.shared.user!.userID
let messageSender = customMessage.sender.userID
let sender = (user == messageSender)
let reuseIdentifier = sender ? "firstCell" : "secondCell"
let myTableView = sender ? tableView : pTableView

myCell = myTableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)