Nick Harris

Archive for December 2015

Swift + UIPickerView + Enums

leave a comment »

I needed a way to power a UIPickerView with an ordered set of options that had different selection values. Its a pretty common thing in HTML and I’ve done it before in Objective-C but I didn’t see any great examples of how to do it in Swift. I really like how this works and shared it on Twitter but thought it deserved a blog post as well.

Feel free to ask me about it. Or if you have suggestions to improve it.

 

enum RecurrenceOptions: Int, CustomStringConvertible {
    case Unknown = 0
    case Daily = 1
    case Weekly = 2
    case Monthly = 3
    case Bimonthly = 4
    case Quarterly = 5
    case Yearly = 6
    case Biannual = 7
    case Biennial = 8
    
    static var count: Int { return RecurrenceOptions.Biennial.rawValue + 1 }
    
    var description: String {
        switch self {
        case .Unknown: return ""
        case .Daily: return "Every Day"
        case .Weekly   : return "Weekly"
        case .Monthly  : return "Every Month"
        case .Bimonthly : return "Twice a Month"
        case .Quarterly : return "Every Three Months"
        case .Yearly : return "Every Year"
        case .Biannual : return "Twice a Year"
        case .Biennial : return "Every Other Year"
        }
    }
    
    var value: Int {
        switch self {
        case .Unknown: return 0
        case .Daily: return 1
        case .Weekly   : return 7
        case .Monthly  : return 30
        case .Bimonthly : return 60
        case .Quarterly : return 90
        case .Yearly : return 360
        case .Biannual : return 180
        case .Biennial : return 720
        }
    }
    
    init?(value: Int) {
        switch value {
        case 0 : self = .Unknown
        case 1 : self = .Daily
        case 7 : self = .Weekly
        case 30 : self = .Monthly
        case 60 : self = .Bimonthly
        case 90 : self = .Quarterly
        case 360 : self = .Yearly
        case 180 : self = .Biannual
        case 720 : self = .Biennial
        default : return nil
        }
    }
}
 
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
 
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        recurrenceLabel.text = ""
        if let recurrence = myCoreDataObject?.recurrance,
           let recurrenceOption = RecurrenceOptions.init(value: recurrence.integerValue) {
            recurrenceLabel.text = recurrenceOption.description
            recurrencePickerView.selectRow(recurrenceOption.rawValue, inComponent: 0, animated: false)
        }
    }
 
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return RecurrenceOptions.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        guard let recurrenceOption = RecurrenceOptions(rawValue: row) else {
            fatalError("Unknown RecurrenceOption")
        }
        
        return choreRecurrenceOption.description
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        guard let recurrenceOption = RecurrenceOptions(rawValue: row) else {
            fatalError(“Unknown RecurrenceOption")
        }
        
        print(“RecurrenceOption.description: \(recurrenceOption.description) | RecurrenceOption.value: \(recurrenceOption.value)")
    }
}
Advertisements

Written by Nick Harris

December 7, 2015 at 9:08 pm

Posted in Uncategorized