swift Swift 2 - FizzBuzz Generator
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift 2 - FizzBuzz Generator相关的知识,希望对你有一定的参考价值。
struct FizzBuzzGenerator: SequenceType {
private let bounds: Range<Int>
func generate() -> AnyGenerator<String> {
var current = bounds.startIndex
return anyGenerator {
guard current < self.bounds.endIndex else {
return nil
}
let output: String
switch (current % 3, current % 5) {
case (0, 0):
output = "FizzBuzz"
case (0, _):
output = "Fizz"
case (_, 0):
output = "Buzz"
default:
output = String(current)
}
current++
return output
}
}
}
let fizzBuzzSequence = FizzBuzzGenerator(bounds: 1...100).lazy
for x in fizzBuzzSequence {
print(x)
}
以上是关于swift Swift 2 - FizzBuzz Generator的主要内容,如果未能解决你的问题,请参考以下文章
swift swift_optional2.swift
Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift
从 Swift 3.0 转换为 Swift 2.3
Swift 新手,无法手动将 swift 2 转换为 swift 5
将 swift 2.3 转换为 swift 3 错误
将版本更改为 Swift 1.2 而不是 Swift 2.1