• Swift


    //: Playground - noun: a place where people can play
    
    import UIKit
    
    enum Month: Int
    {
        // 这么定义, 后面的Feb, Mar会自动赋值为2和3..
        case Jan = 1, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov, Dec
    }
    
    let currentMonth:Month = .Nov
    // 查看枚举对应的值
    currentMonth.rawValue
    
    // 通过这种方法创建出来的枚举变量是一个可选型变量..需要解包后使用
    let nextMonth = Month(rawValue: 12)
    nextMonth!.rawValue
    
    // 示例
    var userInputNumber = 0
    if let theMonth = Month(rawValue: userInputNumber) {
        switch theMonth {
        case .Jan:
            print("It's the first month")
        case .Dec:
            print("It's the last month")
        default:
            print("It's the normal month")
        }
    } else {
        print("ERROR")
    }
    
    // swift中关联的默认值不仅仅是整数, 也可以字符串等其他类型
    enum PersonNum: String
    {
        case one = "Rinpe"
        case two = "Lili"
        case three = "Lolo"
    }
    
    // 示例
    let userInput = "Rinpe"
    if PersonNum(rawValue: userInput) != nil {
        print("欢迎您, (userInput)教授")
    } else {
        print("对不起, 您不是本公司的员工")
    }
    

      

  • 相关阅读:
    记一个在训练模型过程中自己给自己挖的坑
    Fast R-CNN学习总结
    SPP-net论文总结
    R-CNN学习总结
    3Sum Closest
    3Sum
    整数转为罗马数字
    Container With Most Water
    决策树
    回文数判断
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5174729.html
Copyright © 2020-2023  润新知