简体   繁体   English

将嵌套的枚举值转换为一个变量以作为函数参数传递

[英]Convert nested enumeration values into one variable to be passed as function parameters

I'm not sure if this is possible but I was wondering if it's possible to convert nested enumeration values into a single variable so that they can be passed into a function parameter. 我不确定这是否可行,但我想知道是否有可能将嵌套的枚举值转换为单个变量,以便可以将其传递给函数参数。 Example: 例:

enum Food {
  enum Vegetables: String {
    case Spinach    = "Spinach"
    case GreenBeans = "Green Beans"
  }
  enum Fruit: String {
    case Apples    = "Apples"
    case Grapes   = "Grapes"
  }
}

func eat(meal:Food.Vegetables) {
  print("I just ate some \(meal.rawValue)")
}

func eat(meal:Food.Fruit) {
  print("I just ate some \(meal.rawValue)")
}

eat(Food.Fruit.Apples)         //"I just ate some Spinach\n"
eat(Food.Vegetables.Spinach)   //"I just ate some Apples\n"

Everything here works as it should but I'm trying to consolidate my two eat functions into 1. Is there a way to do that? 此处的所有功能均应正常运行,但我试图将我的两个eat函数合并为1。有没有办法做到这一点? I figure it would involve a variable that represents all nested variable types that I could pass into one eat function. 我认为它将涉及一个变量,该变量代表我可以传递给一个eat函数的所有嵌套变量类型。 Something like: 就像是:

func eat(fruitOrVegetable: Food.allNestedEnumeratorTypes) {
  print("I just ate some \(fruitOrVegetable.rawValue)")
}

eat(Food.Vegetables.GreenBeans)   //"I just ate some Green Beans\n"
eat(Food.Vegetables.Grapes)       //"I just ate some Grapes\n"

Is this possible? 这可能吗?

You could use a protocol 您可以使用协议

protocol Vegetarian {
  var rawValue : String { get }
}

and add it to both enums 并将其添加到两个枚举

enum Vegetables: String, Vegetarian { ... }
enum Fruit: String, Vegetarian { ... }

Then you can declare eat 那你就可以eat

func eat(meal:Vegetarian) {
  print("I just ate some \(meal.rawValue)")
}

eat(Food.Vegetables.GreenBeans)   //"I just ate some Green Beans\n"
eat(Food.Fruit.Grapes)            //"I just ate some Grapes\n"

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

相关问题 传递给 function 的一个或多个参数无效 - One or more parameters passed to a function were not valid 传递给函数的一个或多个参数无效。 在解锁 login.keychain - One or more parameters passed to a function were not valid. in unlock login.keychain 如何将委托传递的数据转换为不同的值? - How to convert data passed by delegate to different values? 尝试对作为参数传递给异步函数的数组和对象进行突变 - Trying to mutate array and object passed as parameters to an asynchronous function 为什么不能在没有参数的情况下传递声明为需要参数的swift函数? - Why can a swift function declared as needing parameters, be passed without them? 在一个函数中可变nil - Variable nil in one function SwiftUI 如何检测到有@State 变量值传递给子视图? - How SwiftUI detects there are @State variable values passed to subviews? 枚举作为在块中调用的函数的参数 - Enumeration as Argument for a function called in a block 具有枚举参数(和默认值)的函数 - Function with Enumeration Parameter (and Default value) 在交换机中未处理枚举值'NSFetchedResultsChangeMove'和NSFetchedResultsChangeUpdate' - enumeration values 'NSFetchedResultsChangeMove' and NSFetchedResultsChangeUpdate' not handled in switch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM