Created
January 13, 2019 23:08
-
-
Save kellyhuberty/3a5b7da19bfabae80c147fa39d403af2 to your computer and use it in GitHub Desktop.
I've been using this piece of code to clean up my table view code by storing the section place and definitions in an enum conforming to CaseIterable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CaseIterable+UITableView.swift | |
// | |
// | |
// Created by Kelly Huberty on 12/16/18. | |
// Copyright © 2018 Kelly Huberty. All rights reserved. | |
// | |
import Foundation | |
/** | |
NOTE: this was originally written by me (Kelly Huberty) for another project that I have since | |
re-used elsewhere because it cleans up my table view data sources so well. | |
*/ | |
extension CaseIterable where Self : Equatable{ | |
func index() -> Int{ | |
for (index, aCase) in type(of:self).allCases.enumerated() { | |
if aCase == self { | |
return index | |
} | |
} | |
fatalError() | |
} | |
static func caseAt(index:Int) -> Self? { | |
let cases = Array(allCases) | |
if index < cases.count { | |
return cases[index] | |
} | |
return nil | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment