使用struct,OptionSetType实现oc的enum的多选

//
//  ViewController.swift
//  位移枚举
//
//  Created by zhang on 16/2/24.
//  Copyright © 2016年 jin. All rights reserved.
//
struct Jin:OptionSetType {
    let rawValue:Int
    static let Left = Jin(rawValue: 1<<0)
    static let Right = Jin(rawValue: 1<<1)
    static let Top = Jin(rawValue: 1<<2)
    static let Bottom = Jin(rawValue: 1<<3)
}
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let jin:Jin = [.Bottom,.Left,.Top]
        self.test(jin)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func test(value:Jin)
    {
        if value.contains(.Left){
            print("left")
        }
        if value.contains(.Right){
            print("Right")
        }
        if value.contains(.Top){
            print("Top")
        }
        if value.contains(.Bottom){
            print("Bottom")
        }
    }
}

标签: swift

添加新评论