zoukankan      html  css  js  c++  java
  • swift版的枚举变量

    swift版的枚举变量

    swift的枚举类型跟普通的类是极为类似的,使用的时候,请不要以为他是一个常量,以下是测试用源码

    //
    //  ViewController.swift
    //  SwiftEnum
    //
    //  Created by YouXianMing on 15/10/9.
    //  Copyright © 2015年 ZiPeiYi. All rights reserved.
    //
    
    import UIKit
    
    enum Planet: Int {
        
        case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
    }
    
    enum CompassPoint: String {
        
        case North, South, East, West
    }
    
    enum Barcode {
        
        case UPCA(Int, Int, Int, Int)
        case QRCode(String)
    }
    
    enum MinionIndex: Int {
        
        case DAVE, BOB, JERRY, JORGE, KEVIN, MARK, PHIL, STUART, TIM
        
        static let minionNames = [
            
            DAVE   : "Dave",
            BOB    : "Bob",
            JERRY  : "Jerry",
            JORGE  : "Jorge",
            KEVIN  : "Kevin",
            MARK   : "Mark",
            PHIL   : "Phil",
            STUART : "Stuart",
            TIM    : "Tim"]
        
        func minionName() -> String {
            
            if let minionName = MinionIndex.minionNames[self] {
                
                return minionName
                
            } else {
                
                return "Minion"
            }
        }
        
        func minionImage() -> UIImage? {
            
            return UIImage(named: "Minion(minionName())")
        }
    }
    
    class ViewController: UIViewController {
        
        override func viewDidLoad() {
            
            super.viewDidLoad()
            
            normalExample()
            
            planetExample()
            
            compassPointExample()
            
            barcodeExample()
            
            minionIndexExample()
        }
        
        func normalExample() {
        
            let vegetable = "red pepper"
            
            switch vegetable {
                
            case "celery":
                print("Add some raisins and make ants on a log.")
                
            case "cucumber", "watercress":
                print("That would make a good tea sandwich.")
                
            case let x where x.hasSuffix("pepper"):
                print("Is it a spicy (x)?")
                
            default:
                print("Everything tastes good in soup.")
            }
        }
        
        func planetExample() {
            
            if let planet : Planet = Planet(rawValue: 1) {
                
                // switch 操作
                switch planet {
                    
                case .Mercury:
                    print("(planet) (planet.rawValue)")
                    
                case .Earth:
                    print("(planet) (planet.rawValue)")
                    
                case .Neptune:
                    print("(planet) (planet.rawValue)")
                    
                default:
                    print("(planet) (planet.rawValue)")
                }
                
            } else {
                
                // 没有这个枚举值
                print("no value")
            }
        }
        
        func compassPointExample() {
            
            if let compassPoint : CompassPoint = CompassPoint(rawValue: "Kxo") {
                
                // switch 操作
                switch compassPoint {
                    
                case .North:
                    print("(compassPoint) (compassPoint.rawValue)")
                    
                case .West:
                    print("(compassPoint) (compassPoint.rawValue)")
                    
                default:
                    print("(compassPoint) (compassPoint.rawValue)")
                }
                
            } else {
                
                // 没有这个枚举值
                print("no value")
            }
        }
        
        func barcodeExample() {
        
            let barCode = Barcode.UPCA(20, 120, 10, 20)
            
            switch barCode {
                
            case .UPCA(20, 120, 10, 20):
                print("YES")
                
            default:
                print("NO")
            }
        }
        
        func minionIndexExample() {
        
            print(MinionIndex.DAVE.minionImage())
        }
    }

    rawValue类型的枚举类型

    可以带参数,可以带方法

    非 rawValue 类型

  • 相关阅读:
    浅谈异或运算^的作用
    牛客网剑指offer第40题——数组中只出现一次的数字(浅谈位运算的妙用)
    归并排序——一文吃透归并和递归的思想和完整过程!(没看懂请留言)
    深入分析二分查找及其变体
    vector构造函数的学习
    牛客网剑指offer第34题——找到第一个只出现一次的字符
    牛客网剑指offer第27题——求字符串的全排列
    牛客网剑指offer第20题——定义栈的数据结构
    C++重载运算简介
    Leetcode中字符串总结
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4868541.html
Copyright © 2011-2022 走看看