Find first element matching condition in Swift array (e.g. EKSource)

前端 未结 7 1732
迷失自我
迷失自我 2020-12-24 05:35

I would like to find the first EKSource of type EKSourceType.Local with a \"single\"-line expression in Swift. Here is what I currently have:

7条回答
  •  心在旅途
    2020-12-24 06:27

    For Swift 3 you'll need to make a few small changes to Nate's answer above. Here's the Swift 3 version:

    public extension Sequence {
        func find(predicate: (Iterator.Element) throws -> Bool) rethrows -> Iterator.Element? {
            for element in self {
                if try predicate(element) {
                    return element
                }
            }
            return nil
        }
    }
    

    Changes: SequenceType > Sequence, Self.Generator.Element > Iterator.Element

提交回复
热议问题