https://github.com/bannzai/enumerable
Swift Enum can enumerable.
https://github.com/bannzai/enumerable
Last synced: 12 months ago
JSON representation
Swift Enum can enumerable.
- Host: GitHub
- URL: https://github.com/bannzai/enumerable
- Owner: bannzai
- License: mit
- Created: 2017-08-28T04:59:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-09-04T15:00:54.000Z (almost 8 years ago)
- Last Synced: 2025-07-03T03:03:51.236Z (12 months ago)
- Language: Swift
- Size: 26.4 KB
- Stars: 24
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Enumerable
**Enumrable** can get all the elements of `Enum`.
# Usage
Currently only support `Int`.
Define `YourEnum`.
```swift
enum YourEnum: Int, Enumerable {
case one
case two
case three
case four
}
```
Get all elemnts.
```swift
print(YourEnum.elements) // one, two, three, four
print(YourEnum.count) // 4
```
It is particularly compatible with `TableView` and `CollectionView`.
```swift
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.