通过Swift 3.0中的枚举迭代 [英] Iterating through an Enum in Swift 3.0

查看:157
本文介绍了通过Swift 3.0中的枚举迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的枚举,我想迭代。为此,我已经采用了Sequence和IteratorProtocol,如下面的代码所示。 BTW,这可以拷贝/粘贴到Xcode 8中的游乐场。

  import UIKit 

枚举部分:Int {
case Section0 = 0
case Section1
case Section2
}

扩展部分:序列{
func makeIterator() - > SectionsGenerator {
return SectionsGenerator()
}

struct SectionsGenerator:IteratorProtocol {
var currentSection = 0

mutating func next() >板块? {
guard let item = Sections(rawValue:currentSection)else {
return nil
}
currentSection + = 1
返回项
}
$
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >

但是for-in循环会生成错误消息TypeSections.Type不符合协议Sequence
协议一致性在我的扩展名;那么这段代码有什么问题?



我知道还有其他一些方法可以做到这一点,但我想了解这种方法有什么问题。

谢谢。

解决方案

您可以迭代一个符合序列
协议的类型。因此

  for Sections.Section0 {
print(section)
}

将编译并给出预期的结果。但是,当然这不是
真的是你想要的,因为选择的值是任意的,
值本身不需要在序列中。



据我所知,没有办法迭代一个类型本身,所以

  b $ b print(section)
}

编译。这将要求元类型 Sections.Type 符合
序列。也许有人证明我错了。



你可以做的是定义一个返回序列的类型方法:

 扩展部分{
static func all() - > AnySequence<节> {
return AnySequence {
return SectionsGenerator()
}
}

struct SectionsGenerator:IteratorProtocol {
var currentSection = 0

mutate func next() - >板块? {
guard let item = Sections(rawValue:currentSection)else {
return nil
}
currentSection + = 1
返回项
}


}

Sections.all()中的部分{
print(section)
}


I have a simple enum that I would like to iterate over. For this purpose, I've adopted Sequence and IteratorProtocol as shown in the code below. BTW, this can be copy/pasted to a Playground in Xcode 8.

import UIKit

enum Sections: Int {
  case Section0 = 0
  case Section1
  case Section2
}

extension Sections : Sequence {
  func makeIterator() -> SectionsGenerator {
    return SectionsGenerator()
  }

  struct SectionsGenerator: IteratorProtocol {
    var currentSection = 0

    mutating func next() -> Sections? {
      guard let item = Sections(rawValue:currentSection) else {
        return nil
      }
      currentSection += 1
      return item
    }
  }
}

for section in Sections {
  print(section)
}

But the for-in loop generates the error message "Type 'Sections.Type' does not conform to protocol 'Sequence'". The protocol conformance is in my extension; so, what is wrong with this code?

I know there are other ways of doing this but I'd like to understand what's wrong with this approach.

Thanks.

解决方案

You can iterate over a value of a type which conforms to the Sequence protocol. Therefore

for section in Sections.Section0 {
  print(section)
}

would compile and give the expected result. But of course that is not really what you want because the choice of the value is arbitrary and the value itself not needed in the sequence.

As far as I know, there is no way to iterate over a type itself, so that

for section in Sections {
  print(section)
}

compiles. That would require that the "metatype" Sections.Type conforms to Sequence. Perhaps someone proves me wrong.

What you can do is to define a type method which returns a sequence:

extension Sections {
    static func all() -> AnySequence<Sections> {
        return AnySequence {
            return SectionsGenerator()
        }
    }

    struct SectionsGenerator: IteratorProtocol {
        var currentSection = 0

        mutating func next() -> Sections? {
            guard let item = Sections(rawValue:currentSection) else {
                return nil
            }
            currentSection += 1
            return item
        }
    }

}

for section in Sections.all() {
    print(section)
}

这篇关于通过Swift 3.0中的枚举迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆