2016-10-16 4 views
0

cocoapods로 permissionScope를 설치할 때 신속한 3 구문으로 변환해야했지만 그로 인해 발생하는 대부분의 오류는 매우 간단했습니다. 그러나 시퀀스 확장이 작동하지 않는 6 가지 오류가 있습니다. 정확히.스위프트 2 반복자 확장자가 Swift에서 작동하지 않습니다.

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 
    } 
} 

가 빠른 2의 본질적으로 동일한 사용되며 신속한 3

func requiredAuthorized(_ completion: @escaping (Bool) -> Void) { 
    getResultsForConfig{ (results) -> Void in 
     let result = results 
      .first { $0.status != .authorized } 
      .isNil 
     completion(result) 
    } 
} 

:

는 스위프트 2 버전의 확장 3을 제외하고는 오류가 발생합니다. ambiguous use of 'first(where:)'

답변

1

스위프트 3에서 Sequence은 확장 방법 first(_:)과 매우 유사한 동작을하는 first(where:) 메서드를 사용합니다.

(생성 된 헤더 :)에서

/// 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:) 방법을 사용합니다.

관련 문제