gpt4 book ai didi

ios - 如何检查枚举与值的相等性

转载 作者:搜寻专家 更新时间:2023-10-31 22:54:27 25 4
gpt4 key购买 nike

我有以下枚举:

enum BulletinOption {
case notifications
case share(type: EventType)
}

enum EventType {
case singleEvent(position: Int, text: String)
case multipleEvents(text: String)
}

我创建了一个枚举数组,例如:

var options: [BulletinOption] = [
.notifications,
.share(type: .singleEvent(position: 8, text: "My text"))
]

我想做的是检查选项数组是否包含 .share 枚举(与它关联的类型无关),并将其替换为不同类型的 .share 枚举。

例如

if options.contains(BulletinOption.share) {
// find position of .share and replace it
// with .share(type: .multipleEvents(text: "some text"))
}

我该怎么做?

最佳答案

如果你想同时访问数组索引和对象,那么你可以将 for case 与你的 options 数组一起使用。

for case let (index,BulletinOption.share(_)) in options.enumerated() {
//Change value here
options[index] = .share(type: .multipleEvents(text: "some text"))

//You can also break the loop if you want to change for only first search object
}

关于ios - 如何检查枚举与值的相等性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51786126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com