码迷,mamicode.com
首页 > 编程语言 > 详细

swift版的枚举变量

时间:2015-10-10 23:08:37      阅读:512      评论:0      收藏:0      [点我收藏+]

标签:

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 类型

技术分享

 

swift版的枚举变量

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4868541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!