ios - swift : Enum constant with type and value

标签 ios objective-c enums swift ios8

我知道,枚举常量在swift中应该是这样的

enum CompassPoint {
    case North
    case South
    case East
    case West
}

但是如何给第一个元素赋值,如下面的Objective-C代码

enum ShareButtonID : NSInteger
{
   ShareButtonIDFB = 100,
   ShareButtonIDTwitter,
   ShareButtonIDGoogleplus

}ShareButtonID;

最佳答案

你需要给枚举一个类型然后设置值,在下面的例子中North设置为100,其余的将是101102 等,就像在 CObjective-C 中一样。

enum CompassPoint: Int {
    case North = 100, South, East, West
}

let rawNorth = CompassPoint.North.rawValue // => 100
let rawSouth = CompassPoint.South.rawValue // => 101
// etc.

更新:将toRaw()替换为rawValue

关于ios - swift : Enum constant with type and value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24095741/

相关文章:

rust - Rust 中类似 Java 枚举的行为

java - 如何返回通用枚举的枚举元素列表?

ios - 在 SpriteKit 中使用 CAGradientLayer?

ios - 如何在 uidatepicker View 中将最大日期设置为今天 23 :59?

asp.net-mvc - 如何本地化枚举并使用类似于 Html.SelectListFor<T> 的内容

ios - 使用 MKPinAnnotationView 仅更改信息气泡?

ios - 在 Swift 中为 Firebase 数据结构创建类

ios - 在 iOS 中连接到外部数据库

ios - UIDocumentInteractionController 禁用选项菜单

ios - 使用 UICollectionView 收到内存警告以查看图像