#swift2 #iterator #swift3 #extension-methods
#swift2 #итератор #swift3 #методы расширения
Вопрос:
При установке permissionScope с cocoapods мне пришлось преобразовать синтаксис в 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
}
}
он используется по существу одинаково в swift 2 и swift 3
func requiredAuthorized(_ completion: @escaping (Bool) -> Void ) {
getResultsForConfig{ (results) -> Void in
let result = results
.first { $0.status != .authorized }
.isNil
completion(result)
}
}
За исключением swift 3, я получаю сообщение об ошибке ambiguous use of 'first(where:)'
Ответ №1:
В Swift 3 Sequence
есть метод first(where:)
, который ведет себя очень похоже на ваш метод расширения first(_:)
.
(Из сгенерированного заголовка:)
/// 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:)
метод в стандартной библиотеке.