Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import UIKit
  2.  
  3. class Cell {
  4.  
  5. var name: String
  6.  
  7. init(_ name: String) {
  8. self.name = name
  9. }
  10. }
  11.  
  12. class Row: Sequence, IteratorProtocol {
  13.  
  14. typealias Element = Cell
  15. var iteratorIndex: Int = 0
  16.  
  17. var cells = [Cell]()
  18.  
  19. init(cellNames: String...) {
  20. for name in cellNames {
  21. cells.append(Cell(name))
  22. }
  23. }
  24.  
  25. func next() -> Cell? {
  26.  
  27. if iteratorIndex >= cells.count {
  28. return nil
  29. }
  30.  
  31. defer {
  32. iteratorIndex += 1
  33. }
  34.  
  35. return cells[iteratorIndex]
  36. }
  37.  
  38. func makeIterator() -> Row {
  39. self.iteratorIndex = 0
  40. return self
  41. }
  42. }
  43.  
  44. let row = Row(cellNames: "one", "two", "three")
  45.  
  46. for cell in row {
  47. print(cell.name)
  48. }
  49.  
  50. for (index, cell) in row.enumerated() {
  51. print("[\(index), \(cell.name)]")
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement