10000 GitHub - bannzai/Enumerable: Swift Enum can enumerable.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

bannzai/Enumerable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

0