有技术上的原因使用Swift的无大小写的枚举而不是真实的案例吗? [英] Is there a technical reason to use Swift's caseless enum instead of real cases?

查看:79
本文介绍了有技术上的原因使用Swift的无大小写的枚举而不是真实的案例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Swift来说,这是我的新发现:

Being new to Swift this is what I found:

enum HttpMethod {
  static let post = "POST"
  static let get = "GET"
}

// can assign to string property.
request.httpMethod = HttpMethod.post // --> "POST"

使用无用的枚举的原因而不是结构在阅读但不是我对此感兴趣的东西。

The reason to use a caseless enum and not a struct makes sense to me after reading this but is not the thing I'm interested here.

具有强大的C#背景,这就是我要实现的方式:

Having a strong C# background this is how I would implement it:

enum HttpMethod: String {
  case post = "POST"
  case get = "GET"
}

// I'd even consider this alternatively:
enum HttpMethod: String {
  case post
  case get
}

// Must retrieve string value
request.httpMethod = HttpMethod.post.rawValue // --> "POST" or "post"

第二个版本要求使用 rawValue ,但是它将枚举视为 real 枚举。来自C#,我习惯在枚举值上使用 .ToString()

The second version requires the usage of rawValue but it treats the enum as a real enum. Coming from C# I'm used to using .ToString() on enum values.

这一切刚来吗取决于个人喜好和Swift的约定,使用无大小写的枚举代替实际的案例+ rawValue,还是有另一个(技术上的)理由偏爱第一个而不是第二个?

Is this all just coming down to personal preference and Swift's conventions to use a caseless enum instead of actual cases + rawValue, or is there another (technical) reason to prefer the first over the second version?

推荐答案

带有个案的枚举


在以下情况下,最好创建带有个案的枚举:

Enum with cases

It is better to create an enum with cases in the following scenarios:


  • 这是互斥的

  • 在编译时知道的一组有限值

  • 您是定义它的人(如果在框架中定义了枚举,则无法扩展它以添加更多的案例)


  • 由于值是有限集,因此您可以编写详尽的switch语句

  • 更干净的代码

结构 / class 在fr中定义amework,您想扩展它以添加更多值。

When a struct / class is defined in a framework and you want to extend it to add more values.

使用此方法的示例是 Notification.Name c $ c> Foundation

Example of where this approach is used is Notification.Name in Foundation


  • 快速枚举非常强大

  • 枚举可以具有关联的值

  • 枚举可以具有其他功能。 (如果要定义诸如start,inProgress,finished之类的状态,则可以定义一个名为next的函数,该函数可以返回下一个状态。start.next()

  • 如果您处于场景中值不是互斥的,就像可以将值组合在一起,然后使用OptionSet代替。

  • enums in swift are quite powerful
  • enums can have associated values
  • enums can have other functions. (if you are defining states like start, inProgress, finished, you could define a function called next, which could return the next state. start.next()
  • if you are in a scenario where values are not mutually exclusive, like it could a combination of values then use OptionSet instead

  • 这完全取决于您的意图

  • It all depends on your intent

如果您事先知道这些值并且它们不会改变,然后创建一个枚举

If you know the values before hand and they will not change, then create an enum

如果不可能,则创建 static 值。

If that is not possible then create static values.

如果创建的是静态值,那么您会妥协,因此没有要在枚举中使用它,可以将其定义为 struct ,以便使意图更清晰。

If you are creating static values, you are compromising so you don't have to use it in an enum, you could define it as a struct so that the intent is clearer.

这是目前为止,关于可扩展枚举的快速提议

This is as of now, there is a swift proposal for extendable enums

这篇关于有技术上的原因使用Swift的无大小写的枚举而不是真实的案例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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