【发布时间】:2017-05-29 10:15:32
【问题描述】:
我需要更新存储在Array 中的Enum 的关联值。如何在不知道其索引的情况下访问正确案例的单元格?
enum MessageCell {
case from(String)
case to(String)
case subject(String)
case body(String)
}
var cells = [MessageCell.from(""), MessageCell.to(""), MessageCell.subject(""), MessageCell.body("")]
let recipient = "John"
// Hardcoded element position, avoid this
cells[1] = .to(recipient)
// How to find the index of .to case
if let index = cells.index(where: ({ ... }) {
cells[index] = .to(recipient)
}
【问题讨论】: