Swift 2 迭代器扩展在 Swift 3 中不起作用

Swift 2 Iterator Extensions not working in Swift 3

在使用 cocoapods 安装 permissionScope 时,我不得不转换为 swift 3 语法,由此产生的大多数错误都非常简单,但是我有 6 个错误涉及序列扩展无法正常工作。

这是 swift 2 版本的扩展:

extension SequenceType {
    func first(@noescape includeElement: Generator.Element -> Bool) -> Generator.Element? {
        for x in self where includeElement(x) { return x }
        return nil
    }
}

和swift 3

extension Sequence {
    func first(_ includeElement: (Iterator.Element) -> Bool) -> Iterator.Element? {
        for x in self where includeElement(x) { return x }
        return nil
    }
}

在swift2和swift3

中的用法基本相同
func requiredAuthorized(_ completion: @escaping (Bool) -> Void ) {
    getResultsForConfig{ (results) -> Void in
        let result = results
            .first { [=14=].status != .authorized }
            .isNil
        completion(result)
    }
}

除了 swift 3 我得到一个错误 ambiguous use of 'first(where:)'

在 Swift 3 中,Sequence 有一个方法 first(where:),其行为与您的扩展方法 first(_:).

非常相似

(来自生成的header:)

/// Returns the first element of the sequence that satisfies the given
/// predicate or nil if no such element is found.
///
/// - Parameter predicate: A closure that takes an element of the
///   sequence as its argument and returns a Boolean value indicating
///   whether the element is a match.
/// - Returns: The first match or `nil` if there was no match.
public func first(where predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.Iterator.Element?

删除扩展并使用标准库中的 first(where:) 方法。