Swift 3中的SequenceType的Generator.Element是什么?

7

对于Swift 2.2,我使用以下内容:

extension SequenceType where Generator.Element == Character {

}

但是当我想要转换为Swift 3时,我必须使用Sequence而不是SequenceType

但是

extension Sequence where Generator.Element == Character {

}

yield

使用了未声明的类型“Generator”

那么,如何解决这个问题呢?


1
参见例如 https://github.com/apple/swift-evolution/blob/master/proposals/0006-apply-api-guidelines-to-the-standard-library.md:“所有 API 中的“生成器”概念都更名为“迭代器”。”。 - Martin R
@MartinR,谢谢。由于没有提示,我感到困惑了。 - Vyacheslav
你尝试过在Xcode中使用“编辑->转换->到当前Swift语法”吗?它应该可以自动修复这个问题。 - Martin R
@MartinR,我只想从stackoverflow上转换一段代码。这个功能在这种情况下没有帮助。或者我不知道某些东西。 "To Current Swift Syntax" 只能转换我的所有代码。还是说不行? - Vyacheslav
1个回答

16

对于Swift 3.1,您可以这样做:extension Array where Element == Character - thedp
@thedp:感谢你的反馈。请注意,OP是在请求对Sequence进行扩展,而不是对Array进行扩展。(数组是一个序列,但不是相反的情况。) - Martin R
我明白,可能我错了,但是 OP 实际上想做的是扩展字符串的字符。这个 Swift 3.1 的方法可以解决问题, 是吗? - thedp
我不知道 OP 实际上想做什么。但是字符串 characters 是一个集合,而不是一个数组。- 但你说得对,像 Element == Character 这样的同类型要求现在是可能的。 - Martin R
我有点困惑,那么为什么 extension Array where Element == Character { func foo() {} } 可以用于 "text".characters.foo() - thedp
@thedp:在我的Xcode 8.3.3或Xcode 9 beta 2中无法编译。 - Martin R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接