简体   繁体   English

我想将RawRepresentable的rawValue作为Any返回,但是我仅将其作为Any接收

[英]I want to return a RawRepresentable's rawValue as an Any, but I only receive it as an Any

so I have a function which receives an Any and it checks if the Any is an enum by using reflection: 所以我有一个函数,它接收Any并使用反射检查Any是否为枚举:

func extractRawValue(subject: Any) throws -> Any {
    let mirror = Mirror(reflecting: subject)

    guard let displayStyle = mirror.displayStyle,
        case .`enum` = displayStyle else {
            throw Errors.NoEnum
    }

    // And from here I don't know how to go any further...
    // I wish I could do something like this:
    guard let subject = subject as? RawRepresentable where let rawValue = subject.rawValue as Any else {
        throw Errors.NoRawRepresenable
    }

    return rawValue 
}

Does anyone know how I can accomplish something like that? 有谁知道我能完成这样的事情吗?

I think the Swifty way to do this is to use a protocol for the enums you want to use: 我认为执行此操作的Swifty方法是为要使用的枚举使用协议:

protocol ValueAsAnyable {
    func valueAsAny() -> Any
}

extension ValueAsAnyable where Self: RawRepresentable {
    func valueAsAny() -> Any {
        return rawValue as Any
    }
}

func extractRawValue(subject: Any) throws -> Any {
    let mirror = Mirror(reflecting: subject)

    guard let displayStyle = mirror.displayStyle,
        case .`enum` = displayStyle else {
            throw Errors.NoEnum
    }
    guard let anyable = subject as? ValueAsAnyable else {
        throw Errors.NoRawRepresentable
    }
    return anyable.valueAsAny()
}

let subject: Any = TestEnum.test
let thing = try? extractRawValue(subject: subject) //prints "test"

This should allow you to do what you need, but keep Type safety. 这应该允许您执行所需的操作,但要保持Type安全。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果值是“ RawRepresentable”,如何从“任何”中提取“ rawValue” - How to extract `rawValue` from `Any` if value is `RawRepresentable` 将 Any 转换为 RawRepresentable 其中 RawValue == String - Cast Any to RawRepresentable where RawValue == String 如果元素是 Any,我如何从枚举中获取 rawValue? - How I can get rawValue from enum, if element is Any? 如果让Any到RawRepresentable <String> - if-let Any to RawRepresentable<String> 迅速。 带有可选RawValue的RawRepresentable初始化 - Swift. RawRepresentable init with optional RawValue 编写通用 function 时出现问题,它接受由 CustomStringConvertible 提供的任何 RawRepresentable 值 - Problem writing a generic function that accepts any value that is RawRepresentable by a CustomStringConvertible 如果要在Swift的枚举中将可选字符串传递给“ rawValue:”,是否可以使用“ guard let”? - Can I use “guard let” if you want to pass over an optional string to “rawValue:” in enum in Swift? 为什么我没有在iPhone上收到任何通知? - Why I did not receive any notification on my iPhone? 我正在尝试通过 BLE 连接接收指示,但我无法接收任何数据 - I'm trying to receive indicate via BLE connection, but I can't receive any data 协议“RawRepresentable”要求“init(rawValue:)”在 iOS 13.0.0 和更高版本中可用 - Protocol 'RawRepresentable' requires 'init(rawValue:)' to be available in iOS 13.0.0 and newer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM