Guest User

Untitled

a guest
Apr 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. import Foundation
  2.  
  3. struct Photo {
  4. let no: Int
  5. let name: String
  6. }
  7.  
  8. struct PhotoAlbum: Collection, Sequence, IteratorProtocol {
  9.  
  10. var photos: [Photo]
  11.  
  12. var startIndex = 0
  13. var currentIndex = 0
  14. var endIndex: Int {
  15. return photos.count + 1
  16. }
  17.  
  18. subscript(position: Int) -> Photo {
  19. return photos[position]
  20. }
  21.  
  22. mutating func next() -> Photo? {
  23. defer { currentIndex += 1 }
  24. return photos[count]
  25. }
  26.  
  27. func index(after i: Int) -> Int {
  28. return count + i
  29. }
  30. }
  31.  
  32. let photoAlbum = [Photo(no: 1, name: "sea"), Photo(no: 2, name: "mountain")]
  33. var albumIterator = photoAlbum.makeIterator()
  34. print(albumIterator.next()?.name)
Add Comment
Please, Sign In to add comment