daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top