枚举方法返回动态类型 [英] enum method returning a dynamic type

查看:136
本文介绍了枚举方法返回动态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我有一个字典 [String:Any] 。要处理我使用枚举值创建一个数组的值:

 枚举Foo {
case option1
case option2

func createKey() - > [String] {
switch self {
case .option1:return [scenario1]
case .option2:return [scenario2]
}
}
}

一旦我有这些值,我需要将它们转换成正确的类型能够使用它们。现在我正在使用 if-statements 手动执行,但如果我可以在枚举中创建一个返回正确类型的方法,那么它会减少很多代码。我当前的代码:

  let origin:[String:Any] = [scenario2:someText] 
let选项:Foo = .option2

option.createKey()。forEach {
guard let rawValue = origin [$ 0] else {return}

switch option {
case .option1:
guard let value = rawValue as? int else {return}
print(Value is a Int:,value)
case .option2:
guard let value = rawValue as? String else {return}
print(Value is a String:,value)
}
}

我想要实现的是:

  option.createKey()。 forEach {
guard let rawValue = origin [$ 0] as? option.getType()else {return}
}

这是可能吗? >

解决方案

我认为这里的核心问题是Swift有严格的打字。这意味着在编译时必须知道类型。这显然是合法的:

  let s:Any =howdy
if let ss = s as? String {
print(ss)
}

但这不合法:

  let s:Any =howdy
let someType = String.self
if let ss =作为? someType {// *
print(ss)
}

someType 必须是类型;它不能是在其本身内隐藏一个类型的变量。但这正是您实际要求做的事情。


I have an enum and I'd like to create a method to return a different type for every case.

For example, I have a dictionary [String: Any]. To process the values I'm using the enum to create an array of keys:

enum Foo {
    case option1
    case option2

    func createKey() -> [String] {
        switch self {
        case .option1: return ["scenario1"]
        case .option2: return ["scenario2"]
        }
    }
}

Once I have the values, I need to cast them to a the proper type to be able to use them. Right now I'm doing it manually using if-statements but it would reduce a lot of code if I can somehow create a method in the enum to return the proper type. My current code:

let origin: [String: Any] = ["scenario2": "someText"]
let option: Foo = .option2

option.createKey().forEach {
    guard let rawValue = origin[$0] else { return }

    switch option {
    case .option1:
        guard let value = rawValue as? Int else { return }
        print("Value is an Int:", value)
    case .option2:
        guard let value = rawValue as? String else { return }
        print("Value is a String:", value)
    }
}

What I would like to achieve is something like:

option.createKey().forEach {
    guard let rawValue = origin[$0] as? option.getType() else { return }
}

Is this possible?

解决方案

I think the core of the problem here is that Swift has strict typing. That means types must be known at compile time. This, obviously, is legal:

let s : Any = "howdy"
if let ss = s as? String {
    print(ss)
}

But this is not legal:

let s : Any = "howdy"
let someType = String.self
if let ss = s as? someType { // *
    print(ss)
}

someType must be a type; it cannot be a variable hiding a type inside itself. But that is precisely what, in effect, you are asking to do.

这篇关于枚举方法返回动态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆