gpt4 book ai didi

ios - 检查枚举中是否存在值

转载 作者:行者123 更新时间:2023-12-05 01:08:23 24 4
gpt4 key购买 nike

以下是我的枚举

enum HomeDataType: String, CaseIterable {
case questions = "questions"
case smallIcons = "smallIcons"
case retailers = "retailers"
case products = "products"
case banners = "banners"
case single_product = "single_product"
case single_retail = "single_retail"
case categories = "categories"
case airport = "All_Airport"
case single_banner = "single_banner"
case none = "none"
}

想检查枚举中是否存在值吗?怎么做?

最佳答案

您可以简单地尝试从您的字符串中初始化一个新的枚举案例,或者检查所有案例是否包含与您的字符串相等的 rawValue:

let string = "categories"

if let enumCase = HomeDataType(rawValue: string) {
print(enumCase)
}

if HomeDataType.allCases.contains(where: { $0.rawValue == string }) {
print(true)
}

关于ios - 检查枚举中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66107403/

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