通过Swift 3.0中的枚举迭代

编程入门 行业动态 更新时间:2024-10-21 17:42:07
本文介绍了通过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中的枚举迭代

本文发布于:2023-11-26 17:11:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迭代   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!