All Projects → bannzai → Enumerable

bannzai / Enumerable

Licence: MIT license
Swift Enum can enumerable.

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language
objective c
16641 projects - #2 most used programming language

Enumerable

Enumrable can get all the elements of Enum.

Usage

Currently only support Int.

Define YourEnum.

enum YourEnum: Int, Enumerable {
  case one
  case two
  case three
  case four
}

Get all elemnts.

print(YourEnum.elements) // one, two, three, four
print(YourEnum.count) // 4

It is particularly compatible with TableView and CollectionView.

enum RowType: Int, Enumerable {
  case one
  case two
  case three
  case four
}

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  return RowType.count
}


public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  guard let item = RowType(rawValue: indexPath.item) else {
    fatalError("out of section type: \(indexPath.section), model has sections: \(String(describing: model?.sections))")
  }
  switch item {
    case .one:
    case two:
  ...
  }
  ...
}

License

Enumerable is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].