【问题标题】:Get enum values at index path SWIFT在索引路径 SWIFT 处获取枚举值
【发布时间】:2021-10-06 23:12:39
【问题描述】:

我有这个模型,我在一个 tableview 上使用并填充它:

struct TestSection {
   var title: String
   var options: [TestOption]
}

struct TestOption {
   var field: String
   var placeholder: String
}
var models = [TestSection]()

override func viewDidLoad() {
        super.viewDidLoad()

models.append(TestSection(title: "Test", options: [
            TestOption(placeholder: "Test"),
            TestOption(placeholder: "Test 2"),
            TestOption(placeholder: "Test 3")
        ]))
}

使用委托传回一些数据后,我可以更改特定索引处的模型值,如下所示: 模型[0].options[1].placeholder = value

我想用枚举实现同样的目标。我的带有枚举的模型如下所示:

struct TestSection {
   var title: String
   var options: [TestOptionType]
}

enum TestOptionType {
   case testOneCell(model: TestOptionOne)
   case testTwoCell(model: TestOptionTwo)
   case testThreeCell(model: TestOptionThree)
}

struct TestOptionOne {
   var field: String
   var placeholder: String
}

struct TestOptionTwo {
   var tag: Int
   var placeholder: String
   var handler: (() -> Void)
}

struct TestOptionThree {
   var firstName: String
   var lastName: String
   var handler: (() -> Void)
}
models.append(TestSection(title: "Description", options: [
            .testOneCell(model: TestOptionOne(field: "Test", placeholder: "Test")),
            .testTwoCell(model: TestOptionTwo(tag: 1, placeholder: "Test", handler: {
             ///handle handler
             })),
            .testThreeCell(model: TestOptionThree(firstName: "Test", lastName: "Test", handler: {
             ///handle handler
             }))
        ]))

如何使用 models[0].options[0] 更改我的结构之一的值。当我尝试它时,我只能将 'self' 添加到诸如 'models[0].options[0].self' 这样的选项的末尾,但这不是我想要的。

【问题讨论】:

  • 如果你写了一些代码来改变options[0].field,因为你认为 options[0]是testOneCell的情况,但是当那行被执行时,options[0]是实际上testTwoCell,它没有field 属性?
  • 你需要的是models[0].options[0] = .testOneCell(model: .init(field: "", placeholder: ""))

标签: ios swift uitableview enums


【解决方案1】:

由于TestOption 是一个枚举,models[0].options[0] 的值可能是三种情况之一,并且在每种情况下,都有一个不同的结构值与之关联。

您似乎想要设置特定选项类型之一的属性(TestOptionOne、TestOptionTwo 或 TestOptionThree)。好吧,如果不确保 存在 实际上是与枚举值相关联的那种类​​型的值,您就无法做到这一点。例如,要设置models[0].options[0].field,您必须首先确保models[0].options[0] 是testOneCell 的情况。为此:

if case .testOneCell(var optionOne) = models[0].options[0] {

    // after we do the check, we can set the entire enum, but only changing field to a new value
    models[0].options[0] = .testOneCell(TestOptionOne(
        field: "This is a new value for field", placeholder: optionOne.placeholder
    ))
} else {
    // You should decide what to do when the enum is not of case testOneCell
}

每次都这样做很烦人,所以你可以把它提取到一个变异函数中并放入枚举中:

mutating func setOptionOne(mutator: (inout TestOptionOne) -> Void) {
    if case .testOneCell(var optionOne) = self {
        mutator(&optionOne)
        self = .testOneCell(model: optionOne)
    } else {
        // You should decide what to do when this is called and the enum wasn't of case testOneCell
    }
}

mutating func setOptionTwo(mutator: (inout TestOptionTwo) -> Void) {
    if case .testTwoCell(var optionTwo) = self {
        mutator(&optionTwo)
        self = .testTwoCell(model: optionTwo)
    } else {
        // You should decide what to do when this is called and the enum wasn't of case testTwoCell
    }
}

mutating func setOptionThree(mutator: (inout TestOptionThree) -> Void) {
    if case .testThreeCell(var optionThree) = self {
        mutator(&optionThree)
        self = .testThreeCell(model: optionThree)
    } else {
        // You should decide what to do when this is called and the enum wasn't of case testThreeCell
    }
}

用法:

models[0].options[0].setOptionOne { $0.field = "This is a new value for field" }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多