-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathContents.swift
More file actions
37 lines (29 loc) · 759 Bytes
/
Contents.swift
File metadata and controls
37 lines (29 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//: Playground - noun: a place where people can play
// Powered by https://maimieng.com from https://github.com/kingcos/Swift-X-Design-Patterns
import UIKit
// Crowd 序列
struct Crowd: Sequence {
let end: Int
func makeIterator() -> CrowdIterator {
return CrowdIterator(self)
}
}
// Crowd 迭代器
struct CrowdIterator: IteratorProtocol {
var crowd: Crowd
var times: Int
init(_ crowd: Crowd) {
self.crowd = crowd
times = crowd.end - 1
}
mutating func next() -> Int? {
let nextNumber = crowd.end - times
guard nextNumber <= crowd.end else { return nil }
times -= 1
return nextNumber
}
}
// 迭代
for i in Crowd(end: 10) {
print(i)
}