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

前端 未结 7 1690
迷失自我
迷失自我 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:16

    Swift 4 solution that also handles the situation when there are no elements in your array that match your condition:

    if let firstMatch = yourArray.first{$0.id == lookupId} {
      print("found it: \(firstMatch)")
    } else {
      print("nothing found :(")
    }
    

提交回复
热议问题