swift - 从匹配类型的字符串中获取 Swift 枚举类型,而不是 rawValue
问题描述
我正在使用一个第三方库,它有一个定义一些媒体设备类型的枚举类。我还有一个 API,可以将媒体设备类型作为字符串提供给 Swift 代码。我需要以某种方式从该字符串中获取媒体设备类型,以便可以将其传递给另一个方法。
第三方枚举
@objc public enum MediaDeviceType: Int, CustomStringConvertible {
case audioBluetooth
case audioWiredHeadset
case audioBuiltInSpeaker
case audioHandset
case videoFrontCamera
case videoBackCamera
case other
public var description: String {
switch self {
case .audioBluetooth:
return "audioBluetooth"
case .audioWiredHeadset:
return "audioWiredHeadset"
case .audioBuiltInSpeaker:
return "audioBuiltInSpeaker"
case .audioHandset:
return "audioHandset"
case .videoFrontCamera:
return "videoFrontCamera"
case .videoBackCamera:
return "videoBackCamera"
case .other:
return "other"
}
}
}
我尝试过的事情:
如您所见,.rawValue
这Int
意味着我无法使用枚举的初始化程序访问该值:
let stringType = "audioBluetooth"
let type = MediaDeviceType(rawValue: stringType)
这给了我错误:
无法将“String”类型的值转换为预期的参数类型“Int”
我想也许我可以返回正确switch
的stringType
媒体设备类型,但似乎在传递给下一个方法时它实际上并没有按预期工作。
private func getMediaTypeStringToEnum(type: String) -> MediaDeviceType? {
switch (type) {
case "audioBluetooth":
return .audioBluetooth
case "audioWiredHeadset":
return .audioWiredHeadset
case "audioBuiltInSpeaker":
return .audioBuiltInSpeaker
case "audioHandset":
return .audioHandset
case "videoFrontCamera":
return .videoFrontCamera
case "videoBackCamera":
return .videoBackCamera
case "other":
return .other
default:
return nil
}
}
下一个方法采用两个参数 aString
和一个可选的 MediaDeviceType。如果第二个参数不匹配,则方法切换init
方法。文档。
我的基本实现:
let stringType = "audioBluetooth"
let label = "Media Device Label"
let hopefullyEnumType = getMediaTypeStringToEnum(stringType)
let mediaDevice = MediaDevice(label, hopefullyEnumType)
我对 Swift很陌生,所以也许我在这里遗漏了一些明显的东西?
解决方案
如果您绝对确定将来不会使用新案例扩展枚举案例,那么一种解决方案是遍历所有案例并查看哪些匹配:
private func getMediaTypeStringToEnum(type: String) -> MediaDeviceType? {
let allMediaTypes: [MediaDeviceType] = [.audioBluetooth, .audioWiredHeadset, .audioBuiltInSpeaker, .audioHandset, .videoFrontCamera, .videoBackCamera, .other]
return allMediaTypes.first { $0.description == type }
}
如果您不确定,那么您必须密切关注枚举声明,并在allMediaTypes
发生这种情况时将新案例添加到数组中。
当然,如果枚举是CaseIterable
,那么您的问题将不太容易解决,并且不需要维护代码:
private func getMediaTypeStringToEnum(type: String) -> MediaDeviceType? {
MediaDeviceType.allCases.first { $0.description == type }
}
作为旁注,您还可以在枚举上实现一个可失败的初始化程序,这应该使解决方案更易于使用,并且与枚举更具凝聚力:
extension MediaDeviceType {
init?(string: String) {
// do your thing
}
}
推荐阅读
- java - 如何在两个日期之间获取数据 REST Spring
- reporting-services - 将 SSRS 日期选择器格式更改为 DD/MM/YYYY
- java - 如何正确实现部分唤醒锁?
- angular - 添加时找不到客户端应用程序
- javascript - 将 YYYY-MM-DD 对象转换/解析为字符串 ot 为 JavaScript 日期对象
- python - 如何使用 numpy 从 RTDose(3D 阵列)计算平均剂量和最大剂量?
- laravel - 以前的会话不能在 laravel 5.7 中销毁
- java - 连接消费者后未在 dynamo-db 中创建 KCL 租用表
- php - 将 PHP 函数的输出定向到变量并阻止屏幕输出
- python - 返回行中 3 个最高值的列名